Results 1 to 19 of 19
  1. #1
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Java if-then-else regex

    Hey!
    I need to create a regexp, that will do the following:

    a,a,a,a,c - matches
    c,a,a,a,a - matches
    a,a,a,a,a - doesn't match

    I will be using it in Java. In the place of 'a', can be 'b' - they are equal. Also, in the place there can be any other character. This is what i have came up with:

    ^(?=^(a|b)).*((a|b),){4}(?!(a|b))|(?!(a|b)).*(((a| b),*){4})$

    It fails because it matches the 5 a's. I'm quite new to regexp, so I'm not aware of all the possibilities. It matches the 5 a's, because the first if fails, but the second does not. Maybe there is a simpler way to accomplish this? (Also why are the .* necessary in the middle?) Thanks.
    Last edited by MustSeeMelons; 03-18-2014 at 04:02 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,772
    Rep Power
    5

    Default Re: Java if-then-else regex

    Well, what you have described essentially is match a,a,a,a,c | c,a,a,a,a and failing everything else.

    "(a,a,a,a,c)|(c,a,a,a,a)"

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Java if-then-else regex

    Tried a more simpler approach:

    ((a|b),(a|b),(a|b),(a|b),(?!(a|b)))|(^(?!(a|b)),(a |b),(a|b),(a|b),(a|b))

    All would be well, except that it needs a ',' at the end - aka: v,a,a,a,a,. Why does it need a comma at the end?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,772
    Rep Power
    5

    Default Re: Java if-then-else regex

    I still an uncertain what you are trying to do. Your if-then-else subject doesn't tell me much. Are you trying to match all possible rotations of
    a,a,a,a,c (e.g. a,a,a,c,a, etc)? Is the string part of a line or the entire line? And is this an assignment for a class?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Java if-then-else regex

    Sorry if I have been unclear, will try to describe as accurately as I can.
    There can be 3 types of input patterns:
    1) c,a,a,a,a
    2) a,a,a,a,c
    3) a,a,a,a,a
    In the place of 'a' there can be 'b'. The 'c' character can be any other character. The string is the entire line. I need to write a regexp, that will match (1) & (2), but fail on (3). The regexp in my previous post doesn't match (2) (it matches, if the line would be: c,a,a,a,a,), but matches (1) and fails on (3) as it should. And no, this is not an assignment for a class - I'm in the real world and have to wrap my head around regexp. Also, maybe there is a good read to understand how regexp works? Thanks :)

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Java if-then-else regex

    When you say the the 'c' character can be any other character, do you mean any character that is never 'a' or 'b'? Or do you mean any character that isn't whichever of 'a' or 'b' is in this particular string? Should "baaaa" match?
    Get in the habit of using standard Java naming conventions!

  7. #7
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Java if-then-else regex

    The 'c' character can theoretically be anything, but I'm using it as not a|b. 'baaaa' will fail, because a=b & b=a, so in the end it's the same as 'aaaaa'. Hopefully everything is clear.
    Last edited by MustSeeMelons; 03-18-2014 at 08:07 PM.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,772
    Rep Power
    5

    Default Re: Java if-then-else regex

    I believe the c's and a's are simply describing a pattern. So b,b,b,b,b should not match, b,b,b,b,c can match as can z,z,z,z,d. But e,e,e,e,e cannot match. So 5 of any letter should not much but 4 of one and 1 of another can match.

    Am I correct?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Java if-then-else regex

    I think this involves a concept called "lookaround". I'm not sure if or how Java's built-in regex language supports that. Maybe with the "named capture" mentioned here: Using Regular Expressions in Java
    Get in the habit of using standard Java naming conventions!

  10. #10
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Java if-then-else regex

    Almost there:
    1) z,z,z,z,d - will match, if z=a.
    Looking for only 4 of 'a' or 'b' or if they both form 4 in a row. 5 letters, should match if 4 of a|b are in a row, so there are just two possibilities, the first letter isn't a|b and the rest are or all are a|b except for the last. My regexp almost works, it just needs a ',' at the end of c,a,a,a,a,.

    Made a mistake - 'baaaa' should fail.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,772
    Rep Power
    5

    Default Re: Java if-then-else regex

    Well, if b,a,a,a,a should fail then I still don't know what the requirement is. I came up with this to match a single line of 4 a's and 1 c in any order.

    Java Code:
    Pattern p = Pattern.compile("^(?=(^.,.,.,.,.$))((a,){0,4}c(,a){0,4})$");
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Java if-then-else regex

    I've been trying to describe the requirements, there are just 3 cases:
    1)c,a,a,a,a or c,b,b,b,b or c,a,b,a,b etc.
    2)a,a,a,a,c or b,b,b,b,c or b,a,b,a,c etc.
    3)a,a,a,a,a
    4)a,a,c,a,a
    5)b,b,b,c,b
    Success on (1) and (2), fails on everything else.

  13. #13
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Java if-then-else regex

    Thanks everyone for helping, I've figured it out :)

    ^((a|b),*){4},(?!(a|b)).$|^(?!(a|b)).,((a|b),*){4} $

    This was a good read: regex - Regular expression to match string not containing a word? - Stack Overflow
    Didn't know that there are spaces between characters and negative-lookaheads required a dot at the end to function properly. (That's why my previous example asked for that comma at the end!)

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,772
    Rep Power
    5

    Default Re: Java if-then-else regex

    Interesting, I didn't realize that "c,bbbb", "c,abbb", "c,ba,b,b" were also valid matches. I though the commas were required.

    Not certain what you mean when you say there are spaces between characters (unless you are talking about zero width assertions). Oh, and negative lookaheads do not require . at the end of the pattern. It may be required in your case but not generally.

    Regards,
    Jim
    Last edited by jim829; 03-19-2014 at 03:44 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Java if-then-else regex

    Thank you, you just found a bug in my regexp. The commas are required, but that's not the problem, commas will always be present in the input. If say I replace 'a' and 'b' with numbers, say 1 and 2, all hell breaks loose. Example - 1,1,1,1,12 fails, but it should match as there are four of them in a line and the fifth is garbage. Is there a way to treat 12 as 12 not as '1' and '2'?

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,772
    Rep Power
    5

    Default Re: Java if-then-else regex

    \d is a digit, \d+ is one or more digits.

    Your regex was for a and b. Are you writing separate regexes for each type of character that would take the place of a and b?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Java if-then-else regex

    Yes I am, because that's just how the system works :) But for my luck, they are just 10, where only a changes. Looks like I found a workaround: ^((a|b),*){4},(?!(a$|b)).|^(?!(a|b)).,((a|b),*){4} $
    a$ saved the day. Regexp seems to work in mysterious ways, or I'm just not familiar with it's ways.

  18. #18
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,087
    Rep Power
    6

    Default Re: Java if-then-else regex

    I find regexps I didn't come up with myself so hard to digest. I read that last regexp you just posted and wouldn't be able to just tell you what it would and wouldn't exactly match. Yet I've written far gnarlier regexps myself in the past.

    BTW: that space that is at the end, is that deliberate or just the forum software doing its evil things? Its better to post such things in code tags as jim did earlier.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  19. #19
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Java if-then-else regex

    Same for me, If I haven't written it - I'll possibly wont even understand what it does. And no, that's the forum doing it's thing ;)
    I came up with this: ^(?!\1).+(\d+),\1,\1,\1$
    It should match something like this: 2,1,1,1,1. The first is different and the next four are equal,but of course - it also matches: 2,11,1,1,1 and 2,1,1,1,1,1. No Idea why -.-

Similar Threads

  1. Java RegEx
    By poornasamy in forum New To Java
    Replies: 2
    Last Post: 08-30-2013, 10:41 PM
  2. Java RegEx explanation needed
    By heatblazer in forum New To Java
    Replies: 1
    Last Post: 12-03-2012, 07:12 AM
  3. Regex/Selector with Java
    By grundig1988 in forum New To Java
    Replies: 1
    Last Post: 11-08-2012, 06:32 AM
  4. Java RegEx problem, need help
    By shin_orochi in forum Advanced Java
    Replies: 15
    Last Post: 03-13-2012, 08:48 PM
  5. How to work with REGEX in Java
    By new_Java in forum New To Java
    Replies: 2
    Last Post: 03-31-2011, 04:19 AM

Posting Permissions

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