gpt4 book ai didi

regex - Rails 正则表达式排除符号结束单词之间的空格

转载 作者:数据小太阳 更新时间:2023-10-29 08:29:39 25 4
gpt4 key购买 nike

我正在尝试标记两个 #BLOCK 中的每一行行。

此外,我想排除所有符号 [""," ",{},(),\n]

#BLOCK
#NAME {PC8}
#TYPE GHD3
#PROGRAM "FooBar" (2.0)
#DATE 20190501
#BASE 3740 "TXGH3789"
#BLOCK

现在,我有两个解决方案,但我想将它们合二为一。

我正在使用 Rubular,链接在这里:

示例 1:https://rubular.com/r/bd2AxaHB2QLGpt

示例 2:https://rubular.com/r/vmxm2kugNhnDCS

我已经尝试了这两种解决方案:

  1. (?<=#BLOCK\n)(.*)(?=#BLOCK)这是有效的,它标记了两个 #BLOCK 中的所有内容行。

  2. [^,{},(),""," ",\n]这可以排除这些符号,但不会标记两个 #BLOCK 之间的内容。行。

我怎样才能将两者结合起来以获得我在开头描述的预期结果?

预期结果是 #BLOCK 之间的两个标记行并排除符号,如 [{},(),""," ",\n] .

最佳答案

如果“标记”是指匹配,我猜你可以试试这个。
它使用 \G构造。

(注意 - Ruby 使用 //m 选项来表示全点)

(更新 - 不要让它在没有重新启动的情况下超过下一个 block )

/(?:(?:(?<=\#BLOCK\n)|(?!^)\G))[,{}()"\s]*\K(?!\#BLOCK\b)[^,{}()"\s](?=.*\#BLOCK\b)/m

https://rubular.com/r/TxlU9yhiUJkrok

解释
注意 - 此正则表达式一次匹配一个字符。

 (?:
(?<= \#BLOCK \n ) # A block behind
| # or,
(?! ^ ) # Not the BOS
\G # Start matching where last match left off
)
[,{}()"\s]* # Consume optional punctuation and whitespace
\K # Disregard anything matched so far
(?! \#BLOCK \b ) # Don't go past next block
[^,{}()"\s] # Get a single non-punct nor whitespace char
(?= .* \#BLOCK \b ) # Only if there is a block ahead

要匹配字符 block ,请使用这个。

/(?:(?<=\#BLOCK\n)|(?!^)\G)[,{}()"\s]*\K(?=.+\#BLOCK\b)(?:(?!\#BLOCK\b)[^,{}()"\s])+/m

https://rubular.com/r/kyhqnOtIrmrnJ7

解释

 (?:
(?<= \#BLOCK \n ) # A block behind
| # or,
(?! ^ ) # Not the BOS
\G # Start matching where last match left off
)
[,{}()"\s]* # Consume optional punctuation and whitespace
\K # Disregard anything matched so far
(?= .+ \#BLOCK \b ) # Check that there is a block ahead
(?:
(?! \#BLOCK \b ) # Don't go past next block
[^,{}()"\s] # Get a single non-punct nor whitespace char
)+

关于regex - Rails 正则表达式排除符号结束单词之间的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56711446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com