CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: regex problem

  1. #1
    Join Date
    Nov 2005
    Posts
    102

    regex problem

    Hey, i have a little problem with my regex function.
    Suppose i have the following string: 2+3-4+5


    Code:
            Pattern p = Pattern.compile("");
            // Split input with the pattern
            String[] result = p.split("2+3-4+5");
            
            for (int i=0; i<result.length; i++)
                System.out.println(result[i]);
    What i want to do is split all the positive numbers. So the result should be:
    2
    3
    5

    What i get with my regexp. is:
    2
    3-4
    5

    I dont want it to take the -4 with it. How can i solve this...

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: regex problem

    Please post your regex, that will help.

    From what I understand about regex you can put in a \-[^0-9]+
    Or something like that. Basically, you exclude any combination of numbers ( the ^[0-9]+ part) which are followed by the - (by using \- I believe).

    Here, check out this link.

    And here is an example of it in use.
    Last edited by ProgramThis; March 20th, 2008 at 02:59 PM.

  3. #3
    Join Date
    Nov 2005
    Posts
    102

    Re: regex problem

    Sorry, i accidentally deleted my regex from the code above, but its just a simple as this:

    Pattern p = Pattern.compile("[+]");

    With your regex i get the following result:
    2+3
    +5
    Which isn't exactly what i want, i only need to numbers now one by one stored in my object 'result'.

    2
    3
    5

    like that. Any idea how to do that...?
    BTW, i checked that link of yours, i've alredy used that site earlier today but i just can't figure out how to achieve my goal.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: regex problem

    The split pattern you want is something like this: "\\+|(-\\d+)+\\+".
    This translates as PLUS OR [(MINUS followed by any number of digits) one or more times, followed by PLUS].

    The '+' is escaped because it's a special character, the OR is '|', and any number of digits is '\\d+'. If all your numbers are single digits, you won't need this extra '+' after the '\\d'. The '+' following the parentheses means one or more times. The '-' followed by some digits is repeated one or more times in case there are several negative numbers following each other, then at the end of a negative number sequence we must include the '+' in front of the next positive number (remember that the delimiter is everything between each desired output character).

    This is all documented in the Pattern JavaDocs.

    Optimism is an occupational hazard of programming: testing is the treatment...
    K. Beck
    Last edited by dlorde; March 20th, 2008 at 04:42 PM.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Nov 2005
    Posts
    102

    Re: regex problem

    Hey thanks, it works indeed but. There's just one thing though, this way i also have to check for a * (multiply) or / (divide) character and exclude those:
    So if i would have this now: 2+3-4+5*6/7

    Then it should still give me:
    2
    3
    5

    I've tried to edit your regex to this: "\\+|(-\\d+)+|(*\\d+)+\\+" //This one is still without the divide character

    But that gives me an error. Sorry, i'm really trying to understand regex, but its pretty hard to understand the more advance stuff. Im still trying though

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: regex problem

    The '*' is a special character, so you should escape it if you want it as a literal: '\\*' Any character that can be used as a special character in a pattern must be escaped if you want it as a literal.

    The new requirement is to match '*' and '/' (including the digits following)the same as '-' . So if you replace '-' in my expression with '*' OR '/' OR '-', you should get the result you want. The way to OR characters together is to put them inside square braces: "[]".

    Regular expressions aren't really difficult - just unfamiliar. They require that you can logically express what you need to match, and then follow the regex rules exactly. The first bit is where people usually have trouble - they can't express what they want to match logically. It's hard to do because it's not something we normally have to do. The brain is so good at pattern matching, no conscious analysis is needed. When we need to explain it to a computer, paradoxically it takes a while to figure out just what it is we usually do without thinking

    That language is an instrument of human reason, and not merely a medium for the expression of thought, is a truth generally admitted...
    G. Boole
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured