preg_match to .NET equivalent

12,432

Solution 1

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

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

Solution 2

The @ symbol in .net is syntactic sugar that denotes that the following string will be a literal and is located outside of the quotes. this allows for a string where you do not have to double-escape the backslash character (\). So "c:\\foo\\bar" becomes @"c:\foo\bar"

Share:
12,432
spender
Author by

spender

I give my time here because I get so much more in return. Useful things I've written that might help you: blinq: a modern typescript reimplementation of linq-to-objects over iterable objects BkTree: a c# implementation of a Burkhard-Keller tree for indexing data in metric spaces. ComparerBuilder: A small c# library for easily creating complex IComparer<T> instances that compare multiple properties. See this answer for a rationale. ts-comparer-builder: A typescript library for creating complex "compareFunctions" for use with Array.sort. Very similar to ComparerBuilder above. ts-bin-heap: A typescript binary-heap implementation. Very handy for priority queues, which in-turn are very useful for search algorithms such as A*. Things I've written for other people: pShare client (see also) for Duality solutions: A cross-platform, blockchain and WebRTC based file-sharing platform, written with TypeScript, React and Redux, using electronjs.

Updated on June 20, 2022

Comments

  • spender
    spender almost 2 years

    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.