4

I'm working on a script helping me catching all my localized strings in my project and I'm stuck on a RegEx.

In the following string {{ translateAttr title="button.add_user.title" data-disable-with="button.add_user.disabled" }} I'd like to be able to catch "button.add_user.title" and "button.add_user.disabled" because my curly braces starts with translateAttr attribute.

So far, I've come with this rule \{{2}translateAttr .*=(['"])(.+?)(?=(?<!\\)\1)\1 ?}{2} but as you could see here http://lumadis.be/regex/test_regex.php?id=2362 it is not matching all the occurrences.

A little help here would be much appreciated.

EDIT: Patterns I'd like the regex to match too

{{ translateAttr title="button with \"escaped quotes\" here" data-disable-with='button with single quotes' }}
{{ translateAttr title="want this with ' single quote" "but not this one" }}

EDIT2 : Patterns I don't like to match

{{ title="because no translateAttr" }}
{{ translateAttr "because no something= attribute before" }}

Thanks

guillaumepotier
  • 7,369
  • 8
  • 45
  • 72

1 Answers1

2

Use the below regex which uses the PCRE verb (*SKIP)(*F)

^(?!.*?\{{2}\h*translateAttr).*(*SKIP)(*F)|=(["'])((?:\\\1|(?!\1).)*)\1

DEMO

Explanation

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Whoa, that works ! Could you please explain a bit your regex (or some important parts I missed in my previous one to achieve this multiple catch?) Thanks a lot – guillaumepotier Nov 04 '14 at 00:01
  • Hi. Thanks for your explanation. Unfortunately, your regex fails when a string contains ". I edited my above post to add more cases I want the pattern to catch. Thanks again. Best – guillaumepotier Nov 04 '14 at 09:09
  • Hi @avinash, thanks for your reactivity! This is much better for the two last examples http://regex101.com/r/xY3sK0/10 Unfortunately the rule catches also strings in the above examples and it shouldn't. It should only catch what is in {{ translateAttr }} case. Could you kindly have again a look? Thanks a lot – guillaumepotier Nov 04 '14 at 09:46
  • @guillaumepotier which language are you running? – Avinash Raj Nov 04 '14 at 09:54
  • I'm running PHP with preg_match_all(), mainly to analyze .js, .html and .hbs files looking for internalization keys to translate with poEdit. – guillaumepotier Nov 04 '14 at 09:59
  • use the updated regex and get the string you want from group index 1. – Avinash Raj Nov 04 '14 at 10:07
  • This is nearly perfect ! last thing, string "breaks" if there is a single quote inside and that it starts with double. Example here: http://regex101.com/r/xY3sK0/15 with title="button. ' add_user.title" case. I think that would be pretty all after that! Best – guillaumepotier Nov 04 '14 at 10:21
  • Updated. OMG, you need to provide double accepts for this answer :-) – Avinash Raj Nov 04 '14 at 10:27
  • Ahaha thanks! You Sir really are a true RegEx master! I accepted and upvoted your answer, that's the best I can do. I'll link this issue too in my code when I'll release it open source :) Best – guillaumepotier Nov 04 '14 at 10:49
  • really? My best wishes for your project :-) – Avinash Raj Nov 04 '14 at 10:53
  • Hi @Avinash, just FYI, I opened another tricky regex question here, with a bounty! Maybe your skills could help ;) http://stackoverflow.com/questions/27019696/regex-to-match-specific-functions-and-their-arguments-in-files – guillaumepotier Nov 25 '14 at 16:16
  • @guillaumepotier does the new one is based on js? – Avinash Raj Nov 25 '14 at 16:22
  • Still a PHP parser that parses javascript files. – guillaumepotier Nov 25 '14 at 16:23
  • @guillaumepotier get into here http://chat.stackoverflow.com/rooms/65622/regex-help – Avinash Raj Nov 25 '14 at 16:51