在正则表达式中使用 if 子句
我目前正在使用 vb.net 编写 .net windows 应用程序。
我正在尝试将正则表达式传递给 Regex.Match
以从文章中提取某些文本。如何在正则表达式中编写 if 条件?我读了这个正则表达式备忘单,根据它可以使用 来声明条件,但没有给出示例。
例如,我有以下文本:
"Mary have banana. Mary have apple. Mary have NO pear."
我可以使用以下表达式取出 (1) banana
、(2) apple
和 (3) NO pear
:
mary have (.+?\.)+?
但是如果我只想提取 mary
拥有的水果,即 (1) banana
和 (2) apple
,我想我需要在 (.+?\.)+?
部分添加一个条件,对吗?如何在正则表达式中列出条件?
请各位帮忙,谢谢!
I am currently coding a .net windows app using vb.net.
I am trying to pass a regular expression to Regex.Match
to extract certain texts from an article. How do I write an if condition within a regular expression? I read this regular expression cheat sheet, according to which a condition can be stated using <?()>
, but no example was given.
For example, I have following text:
"Mary have banana. Mary have apple. Mary have NO pear."
I can use the following expression to take out (1) banana
, (2) apple
, and (3) NO pear
:
mary have (.+?\.)+?
But if I want to extract only the fruits that mary
has, namely (1) banana
and (2) apple
, I guess I would need to add a condition in the (.+?\.)+?
part, right? How do I list the condition in a regular expression?
Please assist, thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这里尝试一下:
您可以在这里在线尝试: regexr.com?2thid
第一部分是否定的前瞻断言,这意味着如果存在“Mary has NO”,则此正则表达式将不匹配。否则,它将把“Mary have”后面的单词放入第一个捕获组中。
在 Perlretut 中(假设与 .net 相同)条件部分是解释了,但我认为我的解决方案更简单。
Try this here:
You can try it online here: regexr.com?2thid
The first part is a negative lookahead assertion, that means this regex will not match if there is "Mary have NO". Otherwise it will put the word after "Mary have" into the first capturing group.
Here in the Perlretut (assuming its the same for .net) the condition part is explained, but I think my solution is simpler.
其他人已经为您的具体案例提供了解决方案,因此我将只关注标题中提到的“if 子句”。
.NET 支持使用以下模式的条件。
正则表达式将首先尝试匹配文本表达式(内括号中的部分),如果匹配,则整个表达式将尝试使用管道之前的子表达式进行匹配(
[az]+
) 否则它将尝试使用管道后面的子表达式 ([0-9]+
) 进行匹配。话虽如此,我认为 Stema 建议的负面展望更适合您正在尝试做的事情。
注意:“测试”部分还可以使用任何零宽度断言,例如反向查找。
当然,零宽度前瞻是多余的,因为“测试”表达式始终被视为零宽度。
Others have provided solutions for your specific case, so I'll just focus on the "if clause" mentioned in the heading.
.NET supports conditionals using the following pattern.
The regular expression will first try to match the text expression (the portion in the inner parentheses), if it matches then the over all expression will try to match using the sub expression before the pipe (
[a-z]+
) otherwise it will try to match using the sub expression after the pipe ([0-9]+
).Having said all that, I think the negative look ahead as suggested by stema would be a better fit for what you are trying to do.
Note: the "test" portion can also use any of the zero-width assertions such as the negative look behind.
Of-course a zero-width look ahead is redundant as the "test" expression is always considered zero-width.
这是一个解决方案,您可以使用而无需使用正则表达式的麻烦,但我只能用 C# 回答
别笑XD 只是一个速度修复。只需冲洗并重复其余物品即可
Here is a solution that you can use without the hassle of regular expressions, but I can only answer in C#
Don't laugh XD just a speedfix. Just rinse and repeat for the rest of the items
然后尝试使用
.Split()
方法。分割可能看起来像这个字符串then try using the
.Split()
method. the split will probably look something like thisstring