regexreplaceanki

can I use regex to replace spaces with underscores only in specific capture strings?


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.

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.

I'm not sure what else to try. Sorry if I'm not expressing my question right; I'm very new to this


Solution

  • 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):

    enter image description here

    Screenshot (after):

    enter image description here


    Another option if lookarounds are not supported:

    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):

    enter image description here