Explain this java regex

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Given:

Code:
import java.util.regex.*;
class Regex2 {
   public static void main(String [] args) {
      Pattern p = Pattern.compile(args[0]);
      Matcher m = p.matcher(args[1]);
      boolean b = false;
      while(b = m.find()) {
         System.out.print(m.start() + m.group());
   }
}

And the command line:

java Regex2 "\d*" ab34ef

You get the output

01234456

I understand the 0123445 bit, but where in the blue blazes does the 6 come from?
 
The 0 is an attempt to match the entire string.

matcher starts with 1

ab34ef is m.start = 0
a is m.start = 1
so f is m.start = 6

Though I would have thought the 3 would match??
 
Last edited:
change it to:

Code:
import java.util.regex.*;
class Regex2 {
   public static void main(String [] args) {
      Pattern p = Pattern.compile(args[0]);
      Matcher m = p.matcher(args[1]);
      boolean b = false;
      while(b = m.find()) {
         System.out.println("Start: " + m.start().toString());
         System.out.println("Group: " + m.group());
   }
}
and all will come clear..
 
I don't think that's it.
Code:
character   index    string
------------------------
a              0          0
b              1          01
3              2          01234
4              3          01234
e              4          012344
f              5          0123445
               6          01234456

Middle column is the index, right most column how the string gets built? * will match 0 or more, so at the end it matches nothing, by this time start() is 6.
 
Last edited:
Why do you make it hard for yourself (and others) to read? Seriously, just label the outputs to make it clearer.
 
change it to:

Code:
import java.util.regex.*;
class Regex2 {
   public static void main(String [] args) {
      Pattern p = Pattern.compile(args[0]);
      Matcher m = p.matcher(args[1]);
      boolean b = false;
      while(b = m.find()) {
         System.out.println("Start: " + m.start().toString());
         System.out.println("Group: " + m.group());
   }
}
and all will come clear..

Ah ok so basically it's because it matches nothing at the end by which time start is 6
 
Why do you make it hard for yourself (and others) to read? Seriously, just label the outputs to make it clearer.

typing with one hand at the moment is why

or were you refering to the code? If so I'm just copying an example

Edit: easier to read now I have two hands
 
Last edited:
He means the code, the String that gets printed to the console ("01234456") is very ambigious and doesn't exactly help anyone see what is going on. You should always label String outputs even if it's something trivial, it avoids confusion.

So insted of something like:
01234456

You get something like:
Start: 0
Group:
Start: 1
Group:
etc.


I think that "m.start().toString()" will probably throw up a compilation error though as the Matcher's start() method returns an int which is a primitive and does not have the toString() method, it will print fine without it.
 
He means the code, the String that gets printed to the console ("01234456") is very ambigious and doesn't exactly help anyone see what is going on. You should always label String outputs even if it's something trivial, it avoids confusion.

So insted of something like:
01234456

You get something like:
Start: 0
Group:
Start: 1
Group:
etc.


I think that "m.start().toString()" will probably throw up a compilation error though as the Matcher's start() method returns an int which is a primitive and does not have the toString() method, it will print fine without it.

Yeah it did throw an error. And like I said the code was copied from a sample exam question, they're all like this a complete bitch to read!
 
Back
Top Bottom