Complex Regex Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mbewers1
    New Member
    • Feb 2009
    • 68

    Complex Regex Help

    Hi there

    I'm trying to develop a regular expression for my ASP.NET web page which will test the following strings:

    10.1006/jmbi.1995.0238
    10.1145/1219124.1219132
    10.1109/TSC.2009.4
    10.1177/016555150606490 2

    I've used the following guidelines and have come up with:
    \d{1,2}\w[^\]\w{16, }

    but it's not working. Can anyone suggest a better one?

    Cheers, Matt
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I'm personally terrible at these and have to play for hours to figure out what I actually need. But one of our other members pointed me at this RegEx tester.
    http://regexstorm.net/tester?p=(%3f%...%5b323%5dff-32)

    This way you can tweek your formula in real time to see if it is giving you the results you need.

    Comment

    • mbewers1
      New Member
      • Feb 2009
      • 68

      #3
      Thanks, I'll give it a try

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        10.1006/jmbi.1995.0238
        10.1145/1219124.1219132
        10.1109/TSC.2009.4
        10.1177/016555150606490 2
        Ok so the string needs to start with 2 numbers followed by a "."...are these numbers always 10? If so then the string needs to start with 10.

        To check that a string starts with something you use "^"

        The following will check to see if the string starts with 2 numbers followed by a ".":
        Code:
        ^[0-9]{2}\.
        Notice how I put a "\" in front of the "."
        "." is a special character that represents "any character", so to negate the special meaning for this character I placed a \ in front of it.

        Then after the "." you are expecting 4 numbers followed by a "/":
        Code:
        ^[0-9]{2}\.[0-9]{4}/
        After the / I'm not sure what's going on.
        What is expected here?
        Write it out in words and then translate it into regular expressions format :)

        Here are a couple of resources that I use when working with regular expressions:

        -Frinny

        Comment

        • mbewers1
          New Member
          • Feb 2009
          • 68

          #5
          Thanks Frinny, this has helped me a great deal!

          The first 2 digits in a DOI are always 1 0, yes,
          then there is always a dot
          followed by 4 more digits then a "/".
          After that, there is a random alphanumeric and symbol combination of up to 17 digits

          I have worked with regex's before and am familiar with the sequences need to highlight usage of numbers and letters, but this is the trickiest one I've had to write and I'd really like to show off for my final year project!

          I'll have a go at what you've suggested, thanks again,

          Matt

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            So the last part would be "anything" up to 17 characters (and no longer).

            So, the first part of the string always has to start with 10 followed by a "." followed by 4 numbers followed by a "/".

            So you should have:
            Code:
            ^10\.[0-9]{4}/
            After this point you want to match "anything".
            The "." matches any single character except line break characters \r and \n...

            So you want to have "." repeated 17 times and then you are expecting the end.
            To indicate the end you use "$".

            Code:
            ^10\.[0-9]{4}/.{17}$
            The above indicates that the string that you are trying to match starts with "10." ...followed by 4 numbers...follo wed by a "/"... followed by 17 characters (which brings you to the end of the string).


            Don't know if it works, so you should test it, but when you break it down I think this is what you want.
            Last edited by Frinavale; Apr 5 '10, 12:48 PM. Reason: Corrected my regex explaination *blush*

            Comment

            • mbewers1
              New Member
              • Feb 2009
              • 68

              #7
              With the exception of swapping the ^ and % signs around and changing the last clause to red, minimum of 3, maximum of 17, this is how it looked in the end:

              ^10\.[0-9]{4}/.{3,17}$

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Thanks for posting your answer Mbewers1 :)

                I guess I had my $ and ^'s mixed up yesterday (it's been a while since I've used regular expressions). I edited my posts so that future people aren't confused by my backwards thinking.

                I'm glad you solved your problem.
                Thanks again for pointing out my errors!

                -Frinny

                Comment

                Working...