在正则表达式中使用 if 子句

发布于 2024-10-31 16:03:59 字数 706 浏览 2 评论 0原文

我目前正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冬天旳寂寞 2024-11-07 16:03:59

在这里尝试一下:

Mary\shave\s(?!NO)(\S*)

您可以在这里在线尝试: regexr.com?2thid

第一部分是否定的前瞻断言,这意味着如果存在“Mary has NO”,则此正则表达式将不匹配。否则,它将把“Mary have”后面的单词放入第一个捕获组中。

在 Perlretut 中(假设与 .net 相同)条件部分是解释了,但我认为我的解决方案更简单。

Try this here:

Mary\shave\s(?!NO)(\S*)

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.

日久见人心 2024-11-07 16:03:59

其他人已经为您的具体案例提供了解决方案,因此我将只关注标题中提到的“if 子句”。

.NET 支持使用以下模式的条件。

(?(bob)[a-z]+|[0-9]+)

正则表达式将首先尝试匹配文本表达式(内括号中的部分),如果匹配,则整个表达式将尝试使用管道之前的子表达式进行匹配([az]+) 否则它将尝试使用管道后面的子表达式 ([0-9]+) 进行匹配。

话虽如此,我认为 Stema 建议的负面展望更适合您正在尝试做的事情。

注意:“测试”部分还可以使用任何零宽度断言,例如反向查找。

(?(?<!\s)[a-z]+|[0-9]+)

当然,零宽度前瞻是多余的,因为“测试”表达式始终被视为零宽度。

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.

(?(bob)[a-z]+|[0-9]+)

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.

(?(?<!\s)[a-z]+|[0-9]+)

Of-course a zero-width look ahead is redundant as the "test" expression is always considered zero-width.

街角卖回忆 2024-11-07 16:03:59

这是一个解决方案,您可以使用而无需使用正则表达式的麻烦,但我只能用 C# 回答

 字符串句子 = "玛丽有香蕉 玛丽有苹果 玛丽没有梨";
    if (sentence.Contains("banana"))
    {
        string x=句子.Remove(sentence.IndexOf("香蕉"),"香蕉".Length);
    }

别笑XD 只是一个速度修复。只需冲洗并重复其余物品即可

Here is a solution that you can use without the hassle of regular expressions, but I can only answer in C#

    string sentence = "Mary have banana Mary have apple Mary have NO pear";
    if (sentence.Contains("banana"))
    {
        string x= sentence.Remove(sentence.IndexOf("banana"),"banana".Length);
    }

Don't laugh XD just a speedfix. Just rinse and repeat for the rest of the items

少钕鈤記 2024-11-07 16:03:59

然后尝试使用 .Split() 方法。分割可能看起来像这个字符串

sentence = "Mary have banana Mary have apple Mary have NO pear"; 
string[] brokenUp = sentence.Split(
      new String[] 
      { 
          "first fruit as string variable", 
          "second fruit as string variable", 
          "third fruit as string variable" 
      }, 
      StringSplitOptions.None
);
string newSentence = null;
for (int i = 0; i < brokenUp.Length; i++)
{
    newSentence += brokenUp[i];
}

then try using the .Split() method. the split will probably look something like thisstring

sentence = "Mary have banana Mary have apple Mary have NO pear"; 
string[] brokenUp = sentence.Split(
      new String[] 
      { 
          "first fruit as string variable", 
          "second fruit as string variable", 
          "third fruit as string variable" 
      }, 
      StringSplitOptions.None
);
string newSentence = null;
for (int i = 0; i < brokenUp.Length; i++)
{
    newSentence += brokenUp[i];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文