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 4 of 4

Thread: Java regular expression optimization - help needed

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java regular expression optimization - help needed

    Hi All,
    I am new to Java Regular expression. We are using a pattern for matching a string. We are using this for validating a text field and it meets our requirements. But there is a performance issue in the matching.
    pattern [a-zA-Z0-9]+[ ]?(([_\-][a-zA-Z0-9 ])*)?[_\-]?)+

    1. Input text should start with a-zA-Z0-9.
    2. Space(single) is allowed between words
    3. "_" and "-" are allowed but cannot be consecutive.

    Our problem is, for certain input strings the CPU time goes high and causes hanging the threads. Also we get exceptions. Can anyone please
    help me to optimize the Pattern or suggest a new pattern to solve my issue.

    Exception details
    ============================================
    Hung thread details, all the same:
    [9/28/11 11:40:07:320 CDT] 00000003 ThreadMonitor W WSVR0605W: Thread "WebContainer : 26" (0000004f) has been active for 709755 mi
    lliseconds and may be hung. There is/are 1 thread(s) in total in the server that may be hung.
    at java.util.regex.Pattern$GroupCurly.match(Pattern.j ava:3938)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Branch.match(Pattern.java: 4124)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Curly.match0(Pattern.java: 3801)
    at java.util.regex.Pattern$Curly.match(Pattern.java:3 756)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Loop.match(Pattern.java:43 07)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$BranchConn.match(Pattern.j ava:4090)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$GroupCurly.match0(Pattern. java:4006)
    at java.util.regex.Pattern$GroupCurly.match(Pattern.j ava:3928)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Branch.match(Pattern.java: 4124)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Curly.match0(Pattern.java: 3794)
    at java.util.regex.Pattern$Curly.match(Pattern.java:3 756)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Loop.match(Pattern.java:43 07)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Branch.match(Pattern.java: 4124)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Curly.match0(Pattern.java: 3794)
    at java.util.regex.Pattern$Curly.match(Pattern.java:3 756)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Loop.match(Pattern.java:43 07)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$BranchConn.match(Pattern.j ava:4090)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$GroupCurly.match0(Pattern. java:4006)
    at java.util.regex.Pattern$GroupCurly.match(Pattern.j ava:3928)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Branch.match(Pattern.java: 4124)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Curly.match0(Pattern.java: 3794)
    at java.util.regex.Pattern$Curly.match(Pattern.java:3 756)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Loop.match(Pattern.java:43 07)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$BranchConn.match(Pattern.j ava:4090)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$GroupCurly.match0(Pattern. java:4006)
    at java.util.regex.Pattern$GroupCurly.match(Pattern.j ava:3928)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Branch.match(Pattern.java: 4124)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Curly.match0(Pattern.java: 3794)
    at java.util.regex.Pattern$Curly.match(Pattern.java:3 756)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Loop.match(Pattern.java:43 07)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Branch.match(Pattern.java: 4124)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Curly.match0(Pattern.java: 3794)
    at java.util.regex.Pattern$Curly.match(Pattern.java:3 756)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Loop.match(Pattern.java:43 07)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Branch.match(Pattern.java: 4124)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Curly.match0(Pattern.java: 3801)
    at java.util.regex.Pattern$Curly.match(Pattern.java:3 756)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Loop.match(Pattern.java:43 07)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$BranchConn.match(Pattern.j ava:4090)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$GroupCurly.match0(Pattern. java:4006)
    at java.util.regex.Pattern$GroupCurly.match(Pattern.j ava:3928)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Branch.match(Pattern.java: 4124)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Curly.match0(Pattern.java: 3794)
    at java.util.regex.Pattern$Curly.match(Pattern.java:3 756)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Loop.match(Pattern.java:43 07)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Branch.match(Pattern.java: 4124)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Curly.match0(Pattern.java: 3794)
    at java.util.regex.Pattern$Curly.match(Pattern.java:3 756)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Loop.match(Pattern.java:43 07)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$BranchConn.match(Pattern.j ava:4090)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$GroupCurly.match0(Pattern. java:4006)
    at java.util.regex.Pattern$GroupCurly.match(Pattern.j ava:3928)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Branch.match(Pattern.java: 4124)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Curly.match0(Pattern.java: 3794)
    at java.util.regex.Pattern$Curly.match(Pattern.java:3 756)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Loop.match(Pattern.java:43 07)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Branch.match(Pattern.java: 4124)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Curly.match0(Pattern.java: 3794)
    at java.util.regex.Pattern$Curly.match(Pattern.java:3 756)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Loop.match(Pattern.java:43 07)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Branch.match(Pattern.java: 4124)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Curly.match0(Pattern.java: 3794)
    at java.util.regex.Pattern$Curly.match(Pattern.java:3 756)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Loop.match(Pattern.java:43 07)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$BranchConn.match(Pattern.j ava:4090)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$GroupCurly.match0(Pattern. java:4006)
    at java.util.regex.Pattern$GroupCurly.match(Pattern.j ava:3928)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Branch.match(Pattern.java: 4124)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Curly.match0(Pattern.java: 3794)
    at java.util.regex.Pattern$Curly.match(Pattern.java:3 756)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Loop.match(Pattern.java:43 07)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$BranchConn.match(Pattern.j ava:4090)
    at java.util.regex.Pattern$GroupTail.match(Pattern.ja va:4239)
    at java.util.regex.Pattern$GroupCurly.match0(Pattern. java:4006)
    at java.util.regex.Pattern$GroupCurly.match(Pattern.j ava:3928)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Branch.match(Pattern.java: 4124)
    at java.util.regex.Pattern$Ques.match(Pattern.java:37 03)
    at java.util.regex.Pattern$Curly.match0(Pattern.java: 3794)
    at java.util.regex.Pattern$Curly.match(Pattern.java:3 756)
    at java.util.regex.Pattern$GroupHead.match(Pattern.ja va:4180)
    at java.util.regex.Pattern$Loop.matchInit(Pattern.jav a:4323)
    at java.util.regex.Pattern$Prolog.match(Pattern.java: 4263)
    at java.util.regex.Matcher.match(Matcher.java:1139)
    at java.util.regex.Matcher.matches(Matcher.java:514)

    Thanks
    Deepak


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Java regular expression optimization - help needed

    Why did you add + at the end?
    And one more thing, why did you use [_\-] instead of [_|-]

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Java regular expression optimization - help needed

    1. Input text should start with a-zA-Z0-9.
    You don't need a Pattern for this.
    2. Space(single) is allowed between words
    Use a pattern for this
    3. "_" and "-" are allowed but cannot be consecutive.
    Use a separate pattern for this.

    It would be crazy to attempt all 3 in a single Pattern, especially when any one being wrong is enough to invalidate your input

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java regular expression optimization - help needed

    Please do not double post the same question. Your other post has been removed.

Similar Threads

  1. [SOLVED] Pattern, regex (regular expression) case sensitivity question
    By chronoz13 in forum Java SE APIs
    Replies: 1
    Last Post: September 7th, 2011, 04:16 AM
  2. 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
  3. Regular Expression help
    By medoos in forum Java SE APIs
    Replies: 0
    Last Post: March 19th, 2011, 07:23 PM
  4. Simple Hashsets and regular expression problem !
    By burningflower in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 10th, 2011, 01:43 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