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.
(the)(?:\s)(fair)|(a)(?:\s)(trader)|(offer)(?:\s)(you)$1_$2If you are going to the_fair _ there will _ a fair price(the|a|offer)(?:\s)(fair|trader|you)$1_$2If you are going to the_fair a_trader there will offer_you a_fair priceI'm not sure what else to try. Sorry if I'm not expressing my question right; I'm very new to this
(?<=the)\h+(?=fair)|(?<=a)\h+(?=trader)|(?<=offer)\h+(?=you)_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:
(the)\s+(fair)|(a)\s+(trader)|(offer)\s+(you)$1$3$5_$2$4$6Explanation:
(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):

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With