Issue
I simply am trying to get a Regex expression that will match on the values below:
{{word1}}_{{word2}}_{{word3}} // MATCH
_{{word1}}_{{word2}}_{{word3}}. // NO MATCH
{{word1}}_word{{word2}}_{{word3}} // NO MATCH
{{word1}}_{{word2}}_{{word3}}word4 // NO MATCH
{{word1}}_{{word2}}_{{word3}}_ // NO MATCH
_{{word1}}_{{word2}}_{{word3}}_ // NO MATCH
{{word1}}_{{word2))_{{word3}} // NO MATCH
+{{word1}}-{{word2}}_{{word3} // NO MATCH
So basically, the pattern is two-Curly Braces ({{ and }}) enclosing some word (\w+), separated or delimited by a underscore (_). There should be NO characters before the beginning of the String or at the End.
The issue I was having, is with my Regex, it will match on values that exist outside the String, for example, this is what I was currently using:
\b({{\w+}})(\b_\b)*
So it will strings that are word{{word}}_{{word2}}_ and make them valid
Solution
^{{\w+}}(?:_{{\w+}})*$
EXPLANATION
^ asserts position at start of a line
{{ matches the characters {{ literally (case sensitive)
\w matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
}} matches the characters }} literally (case sensitive)
Non-capturing group (?:_{{\w+}})*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
_{{ matches the characters _{{ literally (case sensitive)
\w matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
}} matches the characters }} literally (case sensitive)
$ asserts position at the end of a line
https://regex101.com/r/UJ0GdY/2
Answered By - earandap
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.