• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Regular Expressions (regex) Question  RSS feed

 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,
So far, my attempts at the following have not been successful. I am trying to write java code (that doesn't use the "split()" method in class "java.lang.String") that can split a string containing a combination of numbers separated by a comma. There may not always be a comma and there will be, at most, two numbers. Examples of the only possible combinations are listed below:
  • ,123 (leading comma)
  • 123, (trailing comma)
  • 12,3 (embedded comma)
  • 123 (no comma)

  • I have also searched the Internet for sample code and although I have found lots of code samples dealing with java regular expressions, I have found no code sample that helps me solve my problem.

    For what it's worth, here is [part of] my code:
    And here is the output:

    I expect "Group 1", above, to be "12" and not just "2". As far as I'm concerned, "Group 2" and "Group 3" (above) are correct.

    Note that I am using JDK 1.4.2_07 on Windows XP.

    Thanks (in advance),
    Avi.
    [ May 16, 2005: Message edited by: Avi Abrami ]
     
    Ragu Ram
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    When I saw
    that doesn't use the "split()" method in class "java.lang.String

    I assumed you use a lower version of java but then you actually use 1.4.2_07

    I dont understand why you are against the usage of split()?

    StringTokenizer is the other option that would come to my mind.

    However if you want a solution using Regex then
     
    Avi Abrami
    Ranch Hand
    Posts: 1141
    1
    Java Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ram,
    If you look at the source code for the "String.split()" method, you will see that it invokes the "Pattern.compile()" method. Now I have read that the "Pattern.compile()" method is resource intensive: in other words, it performs slowly. Now if I am going to be repeatedly calling the "split()" method -- using the exact, same 'pattern' each time -- it seems to me that it will have a large effect on the performance of my application. Hence the idea to call "Pattern.compile()" once only, save the result, and simply re-use it each time. But thank you for requesting that clarification. At least we got the obvious, rhetorical question out of the way quickly. Pardon me for not mentioning my justification for not using the "split()" method, in my original post.

    Cheers,
    Avi.
     
    Stefan Wagner
    Ranch Hand
    Posts: 1923
    Linux Postgres Database Scala
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Avi Abrami:
    Now I have read that the "Pattern.compile()" method is resource intensive: in other words, it performs slowly. Now if I am going to be repeatedly calling the "split()" method ...


    Resource intensive is a vague attribut.
    How many executions do you need per second?
     
    Rajagopal Manohar
    Ranch Hand
    Posts: 183
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    you could try this code it returns the 2 numbers seperated by ",".
    Since it is at most 2 numbers this would be sufficient

    Regards,
    Rajagopal
     
    Alan Moore
    Ranch Hand
    Posts: 262
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The reason you only captured one digit in group(1) is that you put the asterisks outside the parentheses. If you put them inside, your code will yield the expected result.But it probably still won't work correctly in a real application--since everything in the regex is optional, it will always match. Try this one: With this regex, groupCount() will return 5, but at most three of those groups will actually match something (1 2 and 3, or 4 and 5). So, first thing after a successful find(), you need to check whether group(1) returns null; that means it was groups 4 and 5 that matched.

    [ May 17, 2005: Message edited by: Alan Moore ]

    [ May 17, 2005: Message edited by: Alan Moore ] damned smilies!
    [ May 17, 2005: Message edited by: Alan Moore ]
     
    Avi Abrami
    Ranch Hand
    Posts: 1141
    1
    Java Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alan,
    Thank you very much for your reply.

    Cheers,
    Avi.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!