RegEx

Hi there,

i need some help on RegEx. i know them well from coding PERL, but the syntax Iguess, must be different. Does anybody know where to get a list of possible "signs" and their meaning
For example:
^[a-z] at the beginning of an expression means, that the string to check MUST start with an alpha-numeric sign. In C# (i believe) it means that the string should start with anything BESIDES an alpha-numeric sign.

So a list of signs and their meanding would be very helpful!

Thanks,

Finch82.
(Moderator: Thread moved to the Regular Expression Forum and Title tweaked for Search Purposes)


Answer this question

RegEx

  • aydinserkan

    Thank u,

    haven't tried it yet, but I guess it'll work. Big Smile

    Bye,
    Finch.

  • IsabellaTsoglin

    Ur pattern is always true, since it doesn't matter with what the string starts. But the problem is not the pattern, the problem is the syntax. I can an error, when I use "\\" and the pattern won't find the right string when I use "[\\]"...
    This one here works just as it should:

    "^[a-zA-Z_0-9]+.{1}[a-zA-Z_0-9]+[.]jpg$"

    I used a wildcard ".{1}" instead of "\\".
    But why do I get problems with the backslash "\w" instead of "[a-zA-Z_0-9]" didn't work either...
    Maybe u have to escape it by using a special method... don't know.

    Finch


  • etiennemartin

    From what I've seen the RegEx syntax does not differ from what is used in perl , php etc...

    in the example you have given

    ^[a-z] -> match from the beginning of the string. This is the same in any language.
    [^a-z] -> means does not contain a character

    You can use the RegEx workbench here to test your regex http://www.gotdotnet.com/Community/UserSamples/Details.aspx SampleGuid=C712F2DF-B026-4D58-8961-4EE2729D7322


    The RegEx reference I use is from here http://gmckinney.info/resources/regex.pdf

    Hope this helps...

  • NatashaKarlsson

    I got your problem now Big Smile.. I missed it earlier
    should have got it earlier
    Thing is when you give the regex pattern as a string, you have to escape it. yes you are escaping it in the regex, but the pattern itself is a string, so you have to escape it again.
    Like if you have the \\ in your pattern, you have to escape each \ again like \\\\
    so the pattern [\w]+\\[\w]+[.]jpg$ should be written as

    [\\w]+\\\\[\\w]+[.]jpg$

    but the simplest way is to tell the compiler to treat your pattern as a literal like

    Regex = new Regex(@"[\w]+\\[\w]+[.]jpg$");
    so then you dont have to escape it again. Thats a new rule of thumb :)



  • Evan Winslow

    [a-zA-Z_0-9]+\\[a-zA-Z_0-9]+[.]jpg$

    I matched it from the end of the string
    so you will get the filename and the folder

  • JDevitt

    Thanks for ur reply,

    Great links!
    But:
    I want to strip certain parts of a path-address.
    I need "\folder\file.jpg" out of "c:\root\directory\folder\file.jpg"
    My RegEx for this is:
    ^[\\][a-zA-Z_0-9]+[\\][a-zA-Z_0-9]+[.]jpg
    I tried [\w] doesn't work. (Very wired).
    Well, this RegEx returns "file.jpg". I think it's because of the [\\]...
    I used the RegEx Workbench and the result was what I wanted..

    Can anybodey help me
    Here is the exact code:

    public string FormatFilename(string s)

    {

       Regex reg = new Regex("\[a-zA-Z_0-9]+[.]jpg");
       while (!reg.IsMatch(s))
       {

          s = s.Remove(0,1);

       }

          return s;

       }//FormatFilename


  • RegEx