我可以在特定的捕获字符串中使用正则表达式替换空间?

问题描述 投票:0回答:1
如果您要参加博览会的交易者,那里的商人将为您提供公平的价格

into

如果您要去the_fair a_trader,那将为您提供公平的价格

我知道如何以各种方式捕获这些特定字符串

e.g。
(the fair)|(a trader)|(offer you)

或者

(the)(\s)(fair)|(a)(\s)(trader)|(offer)(\s)(you)

但我不知道如何处理捕获组的编号,以便任何一个都会发生相同的替换(到下划线)。
我尝试:
1。
寻找
(the)(?:\s)(fair)|(a)(?:\s)(trader)|(offer)(?:\s)(you)

代替

$1_$2

结果 如果您要去the_fair _那里_将是一个公平的价格

2。 寻找

(the|a|offer)(?:\s)(fair|trader|you)

代替
$1_$2

result

If you are going to the_fair a_trader there will offer_you a_fair price
我不确定还要尝试什么。抱歉,如果我不正确地表达问题;我是这个新的
    

ctrl
+

H

regex windows replace notepad++
1个回答
0
投票
(?<=the)\h+(?=fair)|(?<=a)\h+(?=trader)|(?<=offer)\h+(?=you)
  • 取代:_ tick
  • tick
  • matchcase
    
    
  • tick
    trap周围
    
  • 搜索模式
  • 列表表达式
  • 重新放置
  • 解释:
  • (?<=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(之前):

Screenshot(之后):

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.