最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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
  • 1 If these words are not overlapping, you can use the branch reset feature to reuse the same group index: \b(?|(the) (fair)|(a) (trader)|(offer) (you))\b – bobble bubble Commented 2 days ago
  • FYI: The question was previously tagged Notepad++ regarding the branch reset idea. – bobble bubble Commented 2 days ago
  • Why was the NP+ tag changed to Anki ? If NP+ you would want to do what @bobblebubble suggested using the Branch Reset group regular-expressions.info/branchreset.html which reuses the groups. – sln Commented yesterday
Add a comment  | 

2 Answers 2

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

发布评论

评论列表(0)

  1. 暂无评论