2

I am trying to find digits in a string. I know that in finding a digit is done by \d but when I try it on a sample text like the following:

127.0.0.1 - - [11/Dec/2012:11:57:36 -0500] "GET http:// localhost/ HTTP/1.1" 503 418 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"

using my java code

Pattern test = Pattern.compile("\\d");
testLine = in.readLine(); // basically the text above 
// extract date and time log in and number of times a user has hit the page
numTimesAccess++; // increment number of lines in a count   
System.out.println(test.matcher(testLine).group());
System.out.println(test.matcher(testLine).start());
System.out.println(test.matcher(testLine).end());

I get an error exception stating that No Match is found. Is something wrong with my regular expression patter or in the way I am trying to access the text matching the patterns.

1
  • Thanks I got it now I appreciate everyone's help Commented Dec 12, 2012 at 16:35

4 Answers 4

7

Firstly, you should call Matcher.find() before you invoke Matcher.group()

use "\\d+" as regex if you consider 127 as a whole single digit.

        Pattern p = Pattern.compile("\\d+");
        Matcher m = p.matcher(s);
        while(m.find()){
        System.out.println(m.group() + " " + m.start() + " " + m.end());
        }
Sign up to request clarification or add additional context in comments.

4 Comments

I tried that and still nothing. By the way I am running this code on ubuntu linux if anything.
@jonathan1987 can you post the code that you've tried and also mention the exception as well
sure. Pattern test = Pattern.compile("\\d"); testLine = in.readLine(); // basically the text above System.out.println(test.matcher(testLine).group()); and my exception thrown: Exception in thread "main" java.lang.IllegalStateException: No match found at java.util.regex.Matcher.group(Matcher.java:468) at java.util.regex.Matcher.group(Matcher.java:428) at logfilevarnish.ParseLogFile.main(ParseLogFile.java:23)
@jonathan1987 you should call Matcher.find() before you call group(), check my edit
0

If you really want to find single digits, you need this:

Pattern pattern = Pattern.compile("\\d");
Matcher matcher = pattern.matcher(testline);
while (matcher.find()) {
    System.out.println(matcher.group());
}

if you want to find non-floating-point numbers, change the regex to "\\d+".

3 Comments

I tried that I figured I take out the while look and just run it once. The first time matcher.find() was true and then when I printed it out, I still got an exception thrown.
if you only want the first match, replace the while with if
you get the exception, because you always create a new Matcher instead of using just one.
0

Try using \d* instead of \d+. Have a look at this post-: Finding a Number in a string.

Comments

0

just add and use the simple ktx:

 fun String.digits() = 
      Pattern.compile("\\d+").matcher(this).run { if (find()) group() else "" }!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.