[英]Regex: PHP to C# | Help needed with converting
Problem explained below. 问题解释如下。
PHP PHP
public function DoSomething($content) {
preg_match('/\s*\$\s*(.*)/is', $content, $matches);
if(Count($matches) == 0)
return $content;
else
return false;
}
C# 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;
}
My problem is the regular expression "/\\s*\\$\\s*(.*)/is" . 我的问题是正则表达式“/\\\\* \\ $ \\ 。*(。*)/ is” 。 It is not valid in C#.
它在C#中无效。
How do I write this in .NET? 我如何在.NET中写这个? Do you know a an easier way to get the same php result in C#?
你知道在C#中获得相同php结果的更简单方法吗?
Thanks in advance! 提前致谢!
So to set the pattern
you'll need to preface it with the @
sign to properly escape the backslashes. 因此,要设置
pattern
您需要在其前面加上@
符号以正确地逃避反斜杠。 Secondly, the /{pattern}/{flags}
pattern doesn't work in .NET; 其次,
/{pattern}/{flags}
模式在.NET中不起作用; you need to pull out the pattern and send the analogous flags as RegexOptions
: 你需要拉出模式并发送类似的标志作为
RegexOptions
:
string pattern = @"\s*\$\s*(.*)";
if (Regex.IsMatch(Content, pattern,
RegexOptions.IgnoreCase | RegexOptions.Singleline))
Here's the modified code: 这是修改后的代码:
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;
}
I have removed the forward slashes from around the pattern, and converted the flags to enum values, IgnoreCase
and SingleLine
(Dotall). 我已经从模式周围删除了正斜杠,并将标志转换为枚举值,
IgnoreCase
和SingleLine
(Dotall)。
Regex.IsMatch(subjectString, @"\A\s*\$\s*(.*)\z", RegexOptions.IgnoreCase | RegexOptions.Singleline)
EXPLANATION: 说明:
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»
I have few comments on your conversion: 我对您的转化评论很少:
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;
}
At first point if you just have to check there is a match, you should use Regex.IsMatch
. 首先,如果您只需检查是否匹配,则应使用
Regex.IsMatch
。
To represents literal string we prefix string with @
, which helps to escape the slashes etc. 为了表示文字字符串,我们在字符串前加上
@
,这有助于转义斜杠等。
To specify Regex option you can specify like (?is)
as shorthand representation instead of using function's second parameter. 要指定Regex选项,您可以将
(?is)
指定为速记表示,而不是使用函数的第二个参数。
So the final code should look like: 所以最终的代码应该是这样的:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.