user3319473
user3319473

Reputation: 23

Regex: PHP to C# | Help needed with converting

Problem explained below.

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

My problem is the regular expression "/\s*\$\s*(.*)/is". It is not valid in C#.

How do I write this in .NET? Do you know a an easier way to get the same php result in C#?

Thanks in advance!

Upvotes: 1

Views: 479

Answers (4)

NeverHopeless
NeverHopeless

Reputation: 11233

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;
}
  1. At first point if you just have to check there is a match, you should use Regex.IsMatch.

  2. To represents literal string we prefix string with @, which helps to escape the slashes etc.

  3. To specify Regex option you can specify like (?is) as shorthand representation instead of using function's second parameter.

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

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 99001

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»

Upvotes: 0

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

So to set the pattern you'll need to preface it with the @ sign to properly escape the backslashes. Secondly, the /{pattern}/{flags} pattern doesn't work in .NET; you need to pull out the pattern and send the analogous flags as RegexOptions:

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

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

Upvotes: 2

Steffan Donal
Steffan Donal

Reputation: 2344

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

Upvotes: 1

Related Questions