Thread: What gives?!

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0

    Question What gives?!


    Am completely confused as to why this back-reference won't work. Any ideas?

    (\{[\{%])\s*?([^\}%]*)\s*?(\1)

    It should capture: {{ foo }} or {% bar %}
    If I leave off the back-reference, it captures: {{ foo or {% bar

    ./pjb
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,754
    Rep Power
    9397
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by requinix
    Because \1 will be {{ or {%.
    You know, I realised that about 30 seconds after I posted it!
    My bad :-/

    However, since modifying it to this:

    {({|%|#)\s*?([\w\._]+)\s*?(\1)}
    or even:
    {([{%#])\s*?([\w\._]+)\s*?(\1)}

    It will capture {% foo %} but not {{ bar }}.
    What gives with the back-reference?!

    ./pjb
  6. #4
  7. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,754
    Rep Power
    9397
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by requinix
    Because \1 will be { or %.
    D'oh! TAXI!

    I'll get my coat...

    ./pjb
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0
    Ok, so lets say my now looks like this:

    {({|%|#)\s*?( ... )\s*?(}|%|#)}

    I'm now successfully matching the start and end "braces", but it's the bit in the middle (...) that's causing me to pull my hair out!

    I need it to capture everything in between the braces that doesn't match {{, {% or {#

    So these would be captured:

    {{ foo }}
    {{ ' - %s' % bar if bar }}
    {% if foobar %}

    I'll probably have to slap myself again when someone points out that the answer is incredibly simple :-p

    Thanks,

    ./pjb
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0
    This appears to work:

    {({|%|#)\s*?((?!(?:}|%|#)).*?)\s*?(}|%|#)}

    Is this optimal?
    Thanks,

    ./pjb

IMN logo majestic logo threadwatch logo seochat tools logo