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:
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login