Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java String Regular Expression Help

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Java String Regular Expression Help

    Hi All,

    I have wriittn below regex for two lines.
    String LN1Pattern = "^((?=.{1,35}$)(/([A-Z]{1,5})(|(//[a-zA-Z0-9]+))))$";
    System.out.println("/ABC//FGhiJkl012345".matches(LN1Pattern));
     
    String LN2Pattern = "^(|((\\s+(//[a-zA-Z0-9]{1,33})){1,2}))$";
    System.out.println("".matches(LN2Pattern));

    \\s+ is a newline character.

    But when I combines both as below, its not giving me expected result.

    ^(((?=.{1,35}$)(/([A-Z]{1,5})(|(//[a-zA-Z0-9]+))))(|((\\s+(//[a-zA-Z0-9]{1,33})){1,2})))$

    For string "/ABC//FGhiJkl012345\n//abCD01EF02" - returns False. Expected is True
    I think there is some problem in lookahead placed.

    Please help.


    Thanks
    Raj


  2. #2
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Java String Regular Expression Help

    Quote Originally Posted by Raj4Smile View Post
    I think there is some problem in lookahead placed.
    You're right. The problem is in the positive lookahead: (?=.{1,35}$). Quoting from Pattern (Java Platform SE 7 ), "the regular expression . matches any character except a line terminator unless the DOTALL flag is specified," and your test string has exactly that character - \n. To fix this all you need to do is to replace the regex fragment above with
    (?=.|\n{1,35}$)

    A few comments about your regex:

    1. There appears to be an excessive use of capturing groups, i.e., (regex). Are you using them to visually separate parts of your regex? If so...

    2. Your regex will be far more readable and maintainable if you break up the regex string and comment each logical part, e.g.,
    String LN1Pattern =
        "^(?=.|\n{1,35}$)" +    // Positive lookahead - to match 1-35 characters, including \n, but not other line terminators
        "/[A-Z]{1,5}" +         // Match / and 1-5 characters in the character class 
        "(|//[a-zA-Z0-9]+)$";   // Optionally match // and 1 or more characters in the character class
    This can also help spot mistakes by tallying the regex with the associated comment.

    3. You appear to use (|regex) to denote optional match. It will be better, imo, to use the optional quantifier (?), e.g., (?:regex)?. In this case I used a non-capturing group, i.e., (?:regex) to convey the message that I'm grouping the regex for the quantifier, and not for capturing for later use.

  3. The Following User Says Thank You to jashburn For This Useful Post:

    Raj4Smile (April 24th, 2014)

  4. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Java String Regular Expression Help

    Thanks Jashburn, but I am still struggling after using (?=.|\n{1,35}$). I am much newer to regex patterns.

    I have groups of variable length and total length after combining all groups should not exceed given length say 35.

    What I am trying to implement in regex is SWIFT MT103 Field rules. If possible and you get time please have a look at Field 72.

    Now I am detailing my requirement here:
    Pattern specified for the field value is as below
    It can contain either 1 or two lines. Format for two lines are as below

    First Line
    --------------------
    First Line will contain three groups which are optional to each other. Total length of first line should not exceed 35

    First group will contain Uppercase alphabate value, length for which may vary from 1 to 5
    Second group will contain Uppercase alphanumeric value. Length should not exceed 5.
    Third group will contain characters say SWIFT characters.

    Second Line
    -----------------------
    Third group will contain characters say SWIFT characters. Total Length should not exceed 35 characters.



    Can you please help me here.



    Thanks
    Raj

  5. #4
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Java String Regular Expression Help

    Sure, I can help you with this, but I'll need some help from you too. The requirements you posted are not sufficiently clear, so I'll need a number of test cases - examples of valid field 72 values that exercise the field format per requirement. Also throw in some invalid field 72 values.

    The way I develop regex is I always use TDD, where I'd write the unit test cases for a representative set of positive and negative cases. Only then I'd write the regex to pass my test cases.

    I did try looking into MT103 field/tag 72, but the info I found isn't good enough. E.g., the info at SWIFTStandards - Category 1 - MT 103 - 22. Field 72: Sender to Receiver Information and SWIFTStandards - Category 1 - MT 103 Examples are not sufficient for me to develop my test cases.

    Btw, I found an open source (LGPLv2) library, WIFE - open source library | Free Development software downloads at SourceForge.net, that might be applicable for your purposes. I'm not suggesting that you should use the library (as I like a good regex challenge), but I'd not be fulfilling my duty if I don't mention it...

  6. The Following User Says Thank You to jashburn For This Useful Post:

    Raj4Smile (May 12th, 2014)

  7. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Java String Regular Expression Help

    Hi, I am back from long vacation and will be detailing this requirement shortly.


    Thanks
    Raj

Similar Threads

  1. Regular Expression in Java for HTML pages
    By pari89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 18th, 2014, 08:15 AM
  2. validate a String in java Regular Expression
    By khalidhabib in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 1st, 2013, 04:45 AM
  3. Replies: 3
    Last Post: September 30th, 2011, 08:45 AM
  4. How to remove the regular expression from a string ?
    By srimanta in forum Java Theory & Questions
    Replies: 5
    Last Post: July 20th, 2011, 03:07 PM
  5. Using Regular Expression (regex) in Java Programming
    By lordelf007 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 14th, 2010, 10:29 AM

Tags for this Thread