Results 1 to 3 of 3

Thread: Java RegEx

  1. #1
    poornasamy is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Java RegEx

    I want to use java regex for the following problem.

    Input are either in 2 types of format:
    Format 1: 1 - 24 of 90 items, 25 - 48 of 1780 items etc
    Format 2: 90 items, 1780 items etc

    I want ouput as 90, 1780. Should use only one common regex for these two types. How? Can u please help?
    My exising regex pattern: .*\\s+(\\d*)\\s+item(s)?$.
    Last edited by poornasamy; 08-30-2013 at 04:12 PM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Java RegEx

    - your first .* is greedy, add greedy quantifier -> .*?
    - delete the $. at the end
    -> ".*?\\s+(\\d*)\\s+item(s)?" should work with matcher and find(), but item(s)? is in my opinion not good, because you will add a second group, which you don`t need and which is sometimes null
    -> try ".*?\\s+(\\d*)\\s+item(?:s)?"
    Last edited by eRaaaa; 08-31-2013 at 08:51 AM.

  3. #3
    poornasamy is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Re: Java RegEx

    The correct regex is:
    (\\d*)\\s+(\\w*)$|.*of\\s+(\\d*).*
    Thats all

Similar Threads

  1. Java RegEx explanation needed
    By heatblazer in forum New To Java
    Replies: 1
    Last Post: 12-03-2012, 07:12 AM
  2. Regex/Selector with Java
    By grundig1988 in forum New To Java
    Replies: 1
    Last Post: 11-08-2012, 06:32 AM
  3. Java RegEx problem, need help
    By shin_orochi in forum Advanced Java
    Replies: 15
    Last Post: 03-13-2012, 08:48 PM
  4. 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
  5. RegExPlus - Java regex library
    By CodesAway in forum Java Software
    Replies: 5
    Last Post: 02-28-2010, 08:15 PM

Tags for this Thread

Posting Permissions

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