Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Regex Help

Options
  • 10-06-2008 8:09pm
    #1
    Closed Accounts Posts: 1,819 ✭✭✭


    Hoping someone can help me out with a Regex problem.
    I've never really gotten the hang of Regex properly and I'm a bit stuck on this.

    I have some text that resembles this:

    Here is some text that I need to change

    I would like to extract the "[[would like|" part with a Regex.
    So it's basically "[[" followed by some text stopped by "|".

    I thought I had found the solution with this Regex: /\[\[.+?\]{0}\|/

    However, it will match with "some text that I [[would like|" instead of "[[would like|".

    Anyone know what I'm doing wrong?


Comments

  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    \[\[([^\]]*?)\|
    
    - Match the character “[” literally «\[»
    - Match the character “[” literally «\[»
    - Match the regular expression below and capture its match 
       into backreference number 1 «([^\]]*?)»
       - Match any character that is NOT a “A ] character” «[^\]]*?»
          Between zero and unlimited times, as few times as possible, 
          expanding as needed (lazy) «*?»
    - Match the character “|” literally «\|»
    
    

    If you are doing any serious regex stuff I recommend regex buddy.

    Now that I think of it, it may not match something like

    another


  • Closed Accounts Posts: 1,819 ✭✭✭K!LL!@N


    Excellent, thanks for that!
    I will check out Regex Buddy.
    Thanks for the tip.


  • Closed Accounts Posts: 1,819 ✭✭✭K!LL!@N


    Hobbes wrote: »
    Now that I think of it, it may not match something like

    another

    I don't think that will be a problem as I don't think that situation can arise.


  • Registered Users Posts: 515 ✭✭✭NeverSayDie


    Re regex tools, as a free alternative to Regex Buddy, I've found the Regex Coach very useful over the years. Probably not as powerful though, Regex Buddy does look pretty impressive.
    http://www.weitz.de/regex-coach/


Advertisement