Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_match to .NET equivalent

Tags:

c#

.net

regex

php

I have the following code in PHP:

    if (preg_match('@^[a-z0-9/._-]+$@i', $script)
      && !preg_match('@([.][.])|([.]/)|(//)@', $script))

I'm making the assumption that the predicate for the if statement returns true for the string js/core.js.

How would I translate this to C#? The dumb translation is as follows:

if(Regex.IsMatch(script,"@^[a-z0-9/._-]+$@i")
   && !Regex.IsMatch(script,"@([.][.])|([.]/)|(//)@"))

but I suspect that the @ symbol has meaning associated with it that I can't get to the bottom of. A translation to .NET regex would be nice, but I'm entirely familiar with .NET regex, so an explanation of the relevant syntax differences would suffice.

Many thanks.

like image 629
spender Avatar asked Jan 21 '23 16:01

spender


1 Answers

The @s are just delimiter. You don't need them in .NET

if(Regex.IsMatch(script,"^[a-z0-9/._-]+$", RegexOptions.IgnoreCase)
   && !Regex.IsMatch(script,"([.][.])|([.]/)|(//)"))
like image 185
kennytm Avatar answered Jan 24 '23 05:01

kennytm