C# 正则表达式匹配不在引号中的关键字
如何查找不在字符串中的kewords 例如,如果我有文本: 您好,本文就是一个例子 bla bla bla bla“此文本位于字符串中” “随机字符串”更多文本bla bla bla“foo” 我希望能够匹配不在C# 正则表达式匹配不在引号中的关键字,c#,regex,parsing,C#,Regex,Parsing,如何查找不在字符串中的kewords 例如,如果我有文本: 您好,本文就是一个例子 bla bla bla bla“此文本位于字符串中” “随机字符串”更多文本bla bla bla“foo” 我希望能够匹配不在“内的所有单词文本。在其他方面,我想匹配: 注意:我不想匹配红色突出显示的文本,因为它位于字符串中 可能的解决方案: 我一直在努力,到目前为止我已经做到了: (?)((?”)|文本)(?(q)。*?“|”) 注意,正则表达式将if语句用作:(?(谓词)true alternative
“
内的所有单词文本
。在其他方面,我想匹配:
注意:我不想匹配红色突出显示的文本,因为它位于字符串中
可能的解决方案: 我一直在努力,到目前为止我已经做到了:
(?)((?”)|文本)(?(q)。*?“|”)
注意,正则表达式将if语句用作:(?(谓词)true alternative | false alternative)
因此,正则表达式将显示:
查找“或文本。如果找到”,则继续选择,直到再次找到为止(.*)。如果找到文本,则不执行任何操作
当我运行正则表达式时,我匹配整个字符串。我问这个问题是为了学习。我知道我可以删除所有字符串,然后查找我需要的内容。这可能会变得非常棘手,但有一种潜在的方法可以确保匹配文本和字符串结尾之间有偶数个引号:
text(?=[^"]*(?:"[^"]*"[^"]*)*$)
用要匹配的正则表达式替换text
红细胞:
说明:
text # match the literal characters 'text'
(?= # start lookahead
[^"]* # match any number of non-quote characters
(?: # start non-capturing group, repeated zero or more times
"[^"]*" # one quoted portion of text
[^"]* # any number of non-quote characters
)* # end non-capturing group
$ # match end of the string
) # end lookahead
(?<= # preceded by
^ # start of line
( # either
[^*\r\n]| # not a star or line break
\*(?!\*)| # or a single star (star not followed by another star)
\*\* # or 2 stars, followed by...
([^*\\\r\n] # either: not a star or a backslash or a linebreak
|\\. # or an escaped char
|\*(?!\*) # or a single star
)* # as many times as you want
\*\* # ended with 2 stars
)* # as many times as you want
)
text # then the text
以下是一个答案:
(?<=^([^"]|"[^"]*")*)text
在C#字符串中,这看起来像:
"(?<=^([^\"\r\n]|\"([^\"\\\\\r\n]|\\\\.)*\")*)text"
说明:
text # match the literal characters 'text'
(?= # start lookahead
[^"]* # match any number of non-quote characters
(?: # start non-capturing group, repeated zero or more times
"[^"]*" # one quoted portion of text
[^"]* # any number of non-quote characters
)* # end non-capturing group
$ # match end of the string
) # end lookahead
(?<= # preceded by
^ # start of line
( # either
[^*\r\n]| # not a star or line break
\*(?!\*)| # or a single star (star not followed by another star)
\*\* # or 2 stars, followed by...
([^*\\\r\n] # either: not a star or a backslash or a linebreak
|\\. # or an escaped char
|\*(?!\*) # or a single star
)* # as many times as you want
\*\* # ended with 2 stars
)* # as many times as you want
)
text # then the text
我只需在非引用组中贪婪地匹配文本的in引号,将其过滤掉,然后使用一个捕获组来获得非引用答案,如下所示:
".*(?:text).*"|(text)
您可能希望对单词边界等进行一些细化,但这会让您达到您想要的目的,并且是一个清晰易读的示例。到目前为止,我已经多次使用这些答案,并希望分享解决此问题的替代方法,因为有时我无法实现和使用给定的答案 将任务分解为两个子任务,而不是从某个内容中匹配关键字:
[dbo].[fn_Utils_RegexReplace] ([TSQLRepresentation_WHERE], '''.*?(?<!\\)''', '')
[dbo].[fn_Utils_RegexReplace]([TSQLRepresentation_WHERE],''。*?(?您是否尝试过在线正则表达式生成器,例如:为什么要匹配一个您知道是什么的字符串?您计划如何处理结果。意图对于其他人能够给出适当的答案非常重要。您不需要知道问题的意图就可以回答它。此外,您假设他知道什么字符串是。他只给出示例来演示他正在尝试做的事情,而这些不一定是他最终将要使用的。他正在寻找一个特定的结果,如何使用该结果与我们无关。感谢您的帮助!如果我有:“\r\n text\r\n”bla bla…
不匹配…我猜原因是[^”]
将继续到下一行…@TonoNam:如果您希望它在每行的基础上匹配,那么将[^”]
更改为[^”\r\n]
,并将RegexOptions.Multiline
添加到选项中。(?)?
@"(?<=^([^*\r\n]|\*(?!\*)|\*\*([^*\\\r\n]|\\.|\*(?!\*))*\*\*)*)text"
".*(?:text).*"|(text)
[dbo].[fn_Utils_RegexReplace] ([TSQLRepresentation_WHERE], '''.*?(?<!\\)''', '')