1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Simple regex help

Discussion in 'General Programming Chat' started by smaker, Dec 28, 2011.

  1. smaker

    smaker Newbie

    Joined:
    Jan 13, 2010
    Messages:
    12
    Likes Received:
    0
    I'm trying to seperate the following string

    "200 + 99 = ?"

    I want to create a regex to catch the 200. Right now I have .*(?=\+) but how do I specifiy that the + could also be a minus?
     
  2. shubhamm

    shubhamm Junior Member

    Joined:
    Jan 25, 2010
    Messages:
    107
    Likes Received:
    25
    Occupation:
    Developer
    Location:
    BHW
    why u using Regex is for advanced Match just Use Split Function if you want to get 200 form that string & use delimeter as " " (Space)
     
  3. xpwizard

    xpwizard Junior Member

    Joined:
    Nov 6, 2010
    Messages:
    198
    Likes Received:
    122
    "200 + 99 = ?"

    Regex: (\d*).*(\+|-)

    (\d*) = Group matching any number
    .* = Match any character between the two groups (useful in case there isn't whitespace)
    (\+|-) = Group matching either + or -

    Matches = 200 +
    Group 1 = 200
    Group 2 = +
     
  4. smaker

    smaker Newbie

    Joined:
    Jan 13, 2010
    Messages:
    12
    Likes Received:
    0
    Makes sense so much easier too, I figured it out shortly after anyway

    Thanks