Regex:PHP到C#|转换所需的帮助

本文关键字:帮助 转换 PHP Regex | 更新日期: 2023-09-27 17:59:45

问题解释如下。

PHP

public function DoSomething($content) {
    preg_match('/'s*'$'s*(.*)/is', $content, $matches);
    if(Count($matches) == 0)
        return $content;
    else 
        return false;
}

C#

public static string DoSomething(string Content) {
    if (string.IsNullOrEmpty(Content)) 
        return null;
    string pattern = "pattern needed";
    if (Regex.Match(Content, pattern).Groups.Count > 1)
        return Content;
    else
        return null;
}

我的问题是正则表达式"/''s*''$''s*(.*)/is"。它在C#中无效。

我该怎么写。NET?你知道在C#中获得相同php结果的一种更简单的方法吗?

提前感谢!

Regex:PHP到C#|转换所需的帮助

因此,要设置pattern,您需要在其前面加上@号,以正确地转义反斜杠。其次,/{pattern}/{flags}模式在中不起作用。NET;您需要拉出模式并发送类似的标志RegexOptions:

string pattern = @"'s*'$'s*(.*)";
if (Regex.IsMatch(Content, pattern,
    RegexOptions.IgnoreCase | RegexOptions.Singleline))

这是修改后的代码:

public static string DoSomething(string Content) {
    if (string.IsNullOrEmpty(Content)) 
        return null;
    string pattern = @"'s*'$'s*(.*)";
    if (Regex.Match(Content, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups.Count > 1)
        return Content;
    else
        return null;
}

我已经删除了模式周围的正向斜杠,并将标志转换为枚举值IgnoreCaseSingleLine(Dotall)。

Regex.IsMatch(subjectString, @"'A's*'$'s*(.*)'z", RegexOptions.IgnoreCase | RegexOptions.Singleline)

解释:

Options: Case insensitive; Exact spacing; Dot matches line breaks; ^$ don't match at line breaks; Parentheses capture
Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line) «'s*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “$” literally «'$»
Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line) «'s*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the regex below and capture its match into backreference number 1 «(.*)»
   Match any single character «.*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
'$1
Insert the backslash character «'»
Insert the text that was last matched by capturing group number 1 «$1»

我对您的转换没有什么意见:

public static string DoSomething(string Content) {
    if (string.IsNullOrEmpty(Content)) 
        return null;
    string pattern = "pattern needed";
    if (Regex.Match(Content, pattern).Groups.Count > 1)
        return Content;
    else
        return null;
}
  1. 在第一点上,如果您只需要检查是否匹配,则应该使用Regex.IsMatch

  2. 为了表示文字字符串,我们在字符串前面加上@,这有助于转义斜杠等。

  3. 要指定Regex选项,您可以指定类似(?is)的简写表示,而不是使用函数的第二个参数。

所以最后的代码应该是这样的:

public static string DoSomething(string Content)
{
    if (string.IsNullOrEmpty(Content))
        return null;
    string pattern = @"(?is)'s*'$'s*(.*)";
    if (Regex.IsMatch(Content, pattern))
        return Content;
    else
        return null;
}