简体   繁体   English

如何在 c# 中选择所有包括敏感大小写(正则表达式)的内容?

[英]How do I select all including sensitive case (regex) in c#?

I have a problem with a regex command,我的正则表达式命令有问题,

I have a file with a tons of lines and with a lot of sensitive characters,我有一个包含大量行和许多敏感字符的文件,

this is an Example with all sensitive case 0123456789/*-+.&é"'(-è_çà)=~#{[|`\^@]}²$*ù^%µ£¨¤,;:!?./§<>AZERTYUIOPMLKJHGFDSQWXCVBNazertyuiopmlkjhgfdsqwxcvbn

I tried many regex commands but never get the expected result, I have to select everything from Example to the end我尝试了很多正则表达式命令,但从未得到预期的结果,我必须选择从Example 到最后的所有内容


I tried this command on https://www.regextester.com/ :我在https://www.regextester.com/上尝试了这个命令:

\\sExample(.*?)+ \\s示例(.*?)+

Image of the result here结果的图像在这里

And when I tried it in C# the only result I get was : Example当我在C# 中尝试它时,我得到的唯一结果是: Example

I don't understand why --'我不明白为什么——”

Here's a quick chat about greedy and pessimistic:这是关于贪婪和悲观的快速聊天:

Here is test data:下面是测试数据:

Example word followed by another word and then more

Here are two regex:这是两个正则表达式:

Example.*word
Example.*?word

The first is greedy.第一个是贪心。 Regex will match Example then it will take .* which consumes everything all the way to the END of the string and the works backwards spitting a character at a time back out, trying to make the match succeed.正则表达式将匹配Example然后它将采用.* ,它一直消耗到字符串的 END 的所有内容,并且工作向后一次吐出一个字符,试图使匹配成功。 It will succeed when Example word followed by another word is matched, the .* having matched word followed by another (and the spaces at either end)Example word followed by another word匹配时,它会成功, .* 匹配的word followed by another (以及两端的空格)

The second is pessimistic;二是悲观; it nibbled forwards along the string one character at a time, trying to match.它一次一个字符地沿着字符串向前啃,试图匹配。 Regex will match Example then it'll take one more character into the .*?正则表达式将匹配Example然后它会在.*? wildcard, then check if it found word - which it did.通配符,然后检查它是否找到了word - 它找到了。 So pessimistic matching will only find a single space and the full match in pessimistic mode is Example word所以悲观匹配只会找到一个空格,悲观模式下的完整匹配是Example word

Because you say you want the whole string after Example I recommend use of a greedy quantifier so it just immediately takes the whole string that remains and declares a match, rather than nibbling forwards one at a time (slow)因为你说你想要在 Example 之后的整个字符串,我建议使用一个贪婪的量词,所以它只是立即获取剩余的整个字符串并声明匹配,而不是一次向前一个(慢)

This, then, will match (and capture) everything after Example:然后,这将匹配(并捕获)示例之后的所有内容:

\sExample(.*)

The brackets make a capture group.括号构成一个捕获组。 In c# we can name the group using ?<namehere> at the start of the brackets and then everything that .* matches can be retrieved with:在 c# 中,我们可以使用?<namehere>在方括号的开头命名组,然后可以检索 .* 匹配的所有内容:

Regex r = new Regex("\sExample(?<x>.*)");
Match m = r.Match("Exampleblahblah");
Console.WriteLine(m.Groups["x"].Value); //prints: blahblah 

Note that if your data contains newlines you should note that .请注意,如果您的数据包含换行符,则应注意 . doesn't match a newline, unless you enable RegexOptions.SingleLine when you create the regex不匹配换行符,除非您在创建正则表达式时启用 RegexOptions.SingleLine

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM