I would like to turn spaces into underscores in certain phrases for a regex find and replace. For example I would like this:
If you are going to the fair a trader there will offer you a fair price
into
If you are going to the_fair a_trader there will offer_you a fair price
I know how to capture these specific strings in various ways
e.g.
(the fair)|(a trader)|(offer you)
(the)(\s)(fair)|(a)(\s)(trader)|(offer)(\s)(you)
etc.
but I don't know how to handle the numbering of the capture groups so that the same replacement (space to underscore) will happen to any of them.
I attempted:
1.
- FIND:
(the)(?:\s)(fair)|(a)(?:\s)(trader)|(offer)(?:\s)(you)
- REPLACE:
$1_$2
- RESULT:
If you are going to the_fair _ there will _ a fair price
- FIND:
(the|a|offer)(?:\s)(fair|trader|you)
- REPLACE:
$1_$2
- RESULT:
If you are going to the_fair a_trader there will offer_you a_fair price
I'm not sure what else to try. Sorry if I'm not expressing my question right; I'm very new to this
I would like to turn spaces into underscores in certain phrases for a regex find and replace. For example I would like this:
If you are going to the fair a trader there will offer you a fair price
into
If you are going to the_fair a_trader there will offer_you a fair price
I know how to capture these specific strings in various ways
e.g.
(the fair)|(a trader)|(offer you)
(the)(\s)(fair)|(a)(\s)(trader)|(offer)(\s)(you)
etc.
but I don't know how to handle the numbering of the capture groups so that the same replacement (space to underscore) will happen to any of them.
I attempted:
1.
- FIND:
(the)(?:\s)(fair)|(a)(?:\s)(trader)|(offer)(?:\s)(you)
- REPLACE:
$1_$2
- RESULT:
If you are going to the_fair _ there will _ a fair price
- FIND:
(the|a|offer)(?:\s)(fair|trader|you)
- REPLACE:
$1_$2
- RESULT:
If you are going to the_fair a_trader there will offer_you a_fair price
I'm not sure what else to try. Sorry if I'm not expressing my question right; I'm very new to this
Share Improve this question edited 2 days ago Wiktor Stribiżew 627k41 gold badges496 silver badges611 bronze badges asked 2 days ago mpafpmpafp 493 bronze badges New contributor mpafp is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 3 |2 Answers
Reset to default 3- Ctrl+H
- Find what:
(?<=the)\h+(?=fair)|(?<=a)\h+(?=trader)|(?<=offer)\h+(?=you)
- Replace with:
_
- TICK Match case
- TICK Wrap around
- Search Mode Regular expression
- Replace all
Explanation:
(?<=the) # positive lookbehind, make sure we have "the" before
\h+ # horizontal spaces
(?=fair) # positive lookahead, make sure we have "fair" after
| # OR
(?<=a) # positive lookbehind, make sure we have "a" before
\h+ # horizontal spaces
(?=trader) # positive lookahead, make sure we have "trader" after
| # OR
(?<=offer) # positive lookbehind, make sure we have "ofer" before
\h+ # horizontal spaces
(?=you) # positive lookahead, make sure we have "you" after
Screenshot (before):
Screenshot (after):
Another option if lookarounds are not supported:
- Ctrl+H
- Find what:
(the)\s+(fair)|(a)\s+(trader)|(offer)\s+(you)
- Replace with:
$1$3$5_$2$4$6
- TICK Match case
- TICK Wrap around
- Search Mode Regular expression
- Replace all
Explanation:
(the) # group 1
\s+ # spaces
(fair) # group 2
| # OR
(a) # group 3
\s+ # spaces
(trader) # group 4
| # OR
(offer) # group 5
\s+ # spaces
(you) # group 6
Screenshot (after):
So, when one alternation is successfully scanned, the other groups will all be the null string. You should scan with
(the)\s(fair)|(a)\s(trader)|(offer)\s(you)
and replace with $1$3$5_$2$4$6
\b(?|(the) (fair)|(a) (trader)|(offer) (you))\b
– bobble bubble Commented 2 days agoNotepad++
regarding the branch reset idea. – bobble bubble Commented 2 days ago