正则表达式:PHP到C#|转换需要帮助

时间:2014-04-24 13:30:11

标签: c# php regex

下面解释了问题。

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 *(。*)/是" 。它在C#中无效。

如何在.NET中编写此代码?您是否知道在C#中获得相同php结果的更简单方法?

提前致谢!

4 个答案:

答案 0 :(得分:2)

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

string pattern = @"\s*\$\s*(.*)";

if (Regex.IsMatch(Content, pattern,
    RegexOptions.IgnoreCase | RegexOptions.Singleline))

答案 1 :(得分:1)

以下是修改过的代码:

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)。

答案 2 :(得分:0)

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»

答案 3 :(得分:0)

我对你的转换有一些评论:

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. 要指定正则表达式选项,您可以将(?is)指定为速记表示,而不是使用函数的第二个参数。

  4. 所以最终的代码应该是这样的:

    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;
    }