【问题标题】:Write a regular expression in order to search a substring C#编写正则表达式以搜索子字符串 C#
【发布时间】:2014-09-15 18:19:44
【问题描述】:

我有一个字符串,我想检查并在其中搜索一个子字符串。如果找到子字符串,我想对原始字符串做一些事情。

字符串如下所示:

"\r\radmin@Modem -- *<456> \radmin@Modem -- *<456> "  

目标: 搜索子串模式" -- * " 是否存在于字符串中,返回成功或失败(位数在1之间到无限:1、5、36、76、478、975 等)。

我需要的正则表达式模式是什么?

【问题讨论】:

  • @"-- \*&lt;\d+&gt;" 呢?
  • 是的,它可以正常工作,谢谢!
  • 不要认为我是仇恨者,但这显然是“为我编写代码”的问题——它仍然得到了支持和大量的回应,甚至没有评论“你试过什么?” .有点不对劲,你不觉得吗?
  • @insomnium_ 没错,我的尝试并不相关,因为没有什么对我有用(我还是 Regex 的新手),所以我寻求帮助如何编写我需要的 Regex,我认为这没有任何问题。

标签: c# regex string substring


【解决方案1】:

您可以使用以下代码来检查您的模式是否存在:

 string yourInput = "\r\radmin@Modem -- *<456> \radmin@Modem -- *<456> "  ; 
 string pattern = @"<(\d+)>"; 
 boolean success = Regex.Match(yourInput , pattern, RegexOptions.IgnoreCase).Success ; 

success 将在找到数字时为真。

【讨论】:

  • 谢谢,我会检查的。
【解决方案2】:

使用这个:

var myRegex = new Regex("(?<=<)[0-9]+(?=>)");
string resultString = myRegex.Match(yourString).Value;
Console.WriteLine(resultString);
// matches 456

the Regex Demo 中查看比赛。

说明

  • (?&lt;=&lt;) 的后视断言前面是 &lt;
  • [0-9]+ 匹配一位或多位数字
  • 前瞻(?=&gt;) 断言接下来是&gt;

【讨论】:

  • 这很好用,而且我需要承认它很简单 :) 谢谢!
【解决方案3】:

欢迎使用正则表达式!

你知道正则表达式中的某些字符是特殊字符,需要转义,你可以在这里找到它们:http://www.regular-expressions.info/characters.html

这意味着您的模式的正则表达式将是 \s\-\-\s\*&lt;456&gt;

\s 只是表示空格。

【讨论】:

  • \s 不仅仅是空格。相当于[ \t\n]
  • 是的,但我想让我的回答保持简单。
  • 那为什么要添加错误的信息呢?在这种情况下,您最好用简单的空格替换 \s
【解决方案4】:

你可以使用这个正则表达式

<[1-9][0-9]*>

解释

[1-9]

这部分是1-9的范围,所以你的数字大于0

[0-9]*

数字范围从 0 到 9,* 让您可以拥有任意大小的数字

其他方式: 您也可以对数字使用特殊字符,但这实际上取决于正则表达式语法

\d

【讨论】:

  • 谢谢你的解释:)
【解决方案5】:

使用此模式,您可以匹配字符串:"--\s\*\&lt;\d{3}\&gt;" 注意:如果位数可以更改,请使用:"--\s\*\&lt;\d{MIN,MAX}\&gt;" 其中MINMAX 是可以出现在您的字符串中的位数(在我们有兴趣匹配的部分内)。

using System;
using System.Text.RegularExpressions;

class Example
{
   static void Main()
   {
      string text = "One car red car blue car";
      // This regex will match the pattern you're looking for
      // Since youre new to regexes :) I'll explain it a little:
      // "--" matches "--" literally, "\s" matches the space in between but only once.
      // "\*" matches the "*" and "\<" and "\>" match "<" and ">" respectively
      // "\d" matches a digit 0-9 and "{3}" indicates that there are three digits 
      string pat = @"--\s\*\<\d{3}\>";

      // Instantiate the regular expression object.
      Regex r = new Regex(pat, RegexOptions.IgnoreCase);

      // Match the regular expression pattern against a text string.
      Match m = r.Match(text);
      while (m.Success) 
      {
         // Do something ...
         // Find next match
         m = m.NextMatch();
      }
   }
}

这将允许您在每场比赛的基础上进行任何更改。所以每次你匹配正则表达式时,你都可以对你的字符串做一些事情,然后看看是否有另一个匹配等等......

【讨论】:

    【解决方案6】:

    您可以使用Regex.IsMatch 使用此正则表达式--\\s\\*&lt;\\d+&gt; 来匹配-- *&lt;456&gt; 等字符串

    bool MatchTheNumTag(string str)
    {
        Regex reg = new Regex("--\\s\\*<\\d+>");
        return reg.IsMatch(str);
    }
    

    【讨论】:

      【解决方案7】:

      也许以下内容可以帮助您:

          static void Main(string[] args)
          {
              string originalString= "\r\radmin@Modem -- *<456> \radmin@Modem -- *<456> ";
              Regex reg = new Regex(@"-- \*<[1-9][0-9]*>");
              bool isMatch = reg.IsMatch(originalString);
      
              Console.WriteLine(isMatch);
          }
      

      【讨论】:

        猜你喜欢
        • 2019-10-17
        • 1970-01-01
        • 1970-01-01
        • 2011-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多