I want to find and replace the following string:
<tag a=“x” b=“y” c=“z”/>
However it can present in any order, e.g.
<tag c=“z” b=“y” a=“x”/>
<tag b=“y” a=“x” c=“z”/>
What would be the regex term to find all instances of this string?
I want to find and replace the following string:
<tag a=“x” b=“y” c=“z”/>
However it can present in any order, e.g.
<tag c=“z” b=“y” a=“x”/>
<tag b=“y” a=“x” c=“z”/>
What would be the regex term to find all instances of this string?
I believe the regex query that you want is:
<tag ([a-z]){1}=“([a-z]){1}” ([a-z]){1}=“([a-z]){1}” ([a-z]){1}=“([a-z]){1}”/>
Let me help to explain the elements of this query:
([a-z])
Will match any string between a to z{1}
will tell the query that you want to match this query just once!([a-z]){1}
will match any string between a to z just once.If we use this element in this example the matched strings will be:
<tag a=“x” b=“y” c=“z”/>
matched strings: t,a,g,a,x,b,y,c in that order.
If you add your string structure to your query:
tag ([a-z]){1}=“([a-z]){1}”
matched strings: tag a=“x“
Hope this helps!
This is one way:
^<tag +([abc])=“([xyz])“ +(?!\1)([abc])=“(?!\2)([xyz])“ +(?!\1|\3)[abc]=“(?!\2|\4)[xyz]“\/>$
^ # match beginning of line
<tag # match '<tag'
+ # match 1+ spaces
([abc]) # match 'a', 'b' or 'c' in cap group 1
=“ # match '=“'
([xyz]) # match 'x', 'y' or 'z' in cap group 2
“ + # match '“' followed by 1+ spaces
(?!\1) # following cannot match contents of cap group 1
([abc]) # match 'a', 'b' or 'c' in cap group 3
=“ # match '=“'
(?!\2) # following cannot match contents of cap group 2
([xyz]) # match 'x', 'y' or 'z' in cap group 4
“ + # match '“' followed by 1+ spaces
(?!\1|\3) # following cannot match contents of cap group 1 or 3
[abc]=“ # match 'a', 'b' or 'c' followed by '=“'
(?!\2|\4) # do not match contents of cap group 2 or 4
[xyz]“\/> # match 'x', 'y' or 'z' followed by '“/>'
$ # match end of line