Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Find digits separated with a /

Last post 11-18-2009, 5:55 PM by kminev. 4 replies.
Sort Posts: Previous Next
  •  11-18-2009, 11:27 AM 57391

    Find digits separated with a /

    Hi,

     

    I need to have a regex that looks for any digit before and after the char / 

     

    for example This is my string1/232

     

    I need to be able to extract 1/232

     

    I am coding on java.

     

    Any help will be appreciated.

     

    I can probably just split on "/" and then for back and forth until next char is no digit, but I was wondering if there is something better that regex can offer.

     

    Thanks

  •  11-18-2009, 12:41 PM 57392 in reply to 57391

    Re: Find digits separated with a /

    I don't know how Java rolls but as long as your delimiter(php is most often /, I use the most) is different you shouldn't have any problems else you'll need to escape it (php \/) and have your character class on either side of it with proper repetition (one or more or fixed)
  •  11-18-2009, 12:44 PM 57393 in reply to 57392

    Re: Find digits separated with a /

    forgot to add group your character classes since you want to extract and you'll have \1 and \2
  •  11-18-2009, 5:02 PM 57402 in reply to 57391

    Re: Find digits separated with a /

    Try something like:

    (\d+)/(\d+)

    as the pattern. The digit characters before the "/" will be in match group #1 and those after will be in match group #2.

    As jewbacca mentioned, the "/" character is often used as a pattern delimiter so there are 2 options: either use something else as the pattern delimiter or escape the "/".

    Therefore the line might look something like:

    line =~ #(\d+)/(\d+)#;

    or

    line =~ /(\d+)\/(\d+)/;

    (I don't use java so the syntax might not be 100% but you should get the idea).

    Susan

  •  11-18-2009, 5:55 PM 57403 in reply to 57402

    Re: Find digits separated with a /

    Thank you very much this does help. I will polish it up and will post my code as soon I am done.

     

    Thanks

View as RSS news feed in XML