Using OR in replace. How to adress given group?
-
Hello,
When using find, you can do something like (X|Y) and notepad will find both of these. How can i adress these alternative groups in replace?
A simplified example: Lets say we want to swap true and false in these lines:
A candy is true .
A candy is false .
A candy is true .
A candy is false .
A candy is true .Find: (true)|(false)
I tried using something like \2\1 or \1\2 in replace, but then it does nothing?
How can i set replace option for each alternative?
Again, this is only simplified example so 3-step replace is not satisfying :) -
@Adam-Prochowski said:
When using find, you can do something like (X|Y) and notepad will find both of these
find what : A candy is .*$
Search mode: Regular expression
-
@gurikbal-singh said:
find what : A candy is .*$
Search mode: Regular expression
This changes whole subject to a small regex workaround that doesnt work as indented. I Want to have both alternatives and i want to replace based on which alternative is found. So if you find “true” replace with false, if you find “false” replace with true. this is again simplified. Once i get this to work, the alternatives will be contucted of regexes and the replace will be also based on groups i find using regex. I only pinpointed the part i have trouble with - How to adress alternative in find - so that people don’t go into understanding unnecessary commands when question is simple.
Hope this clarifies the problem.
-
Try a regular expression replace of
(A candy is true)|(A candy is false)
with
(?1A candy is false)(?2A candy is true) -
@AdrianHHH said:
Try a regular expression replace of
(A candy is true)|(A candy is false)
with
(?1A candy is false)(?2A candy is true)Thanks! This works :) . I went to this page before but i must’ve misunderstood
"For example, the format string “(?1foo:bar)” will replace each match found with “foo” if the sub-expression $1 was matched, and with “bar” otherwise. "
I tried to use it with “:” between alternatives, but that didn’t quite work. Your solution with (?N smg ) works just right.
-
A couple of more good references to this technique: