Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Regex Help

  • 10-06-2008 08: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, Registered Users 2 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, Registered Users 2 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