Search code examples
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.

  • (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


Solution

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

    enter image description here

    Screenshot (after):

    enter image description here


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

    enter image description here