Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question regex

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.regex.*;
import java.io.*;

public class Regexdemo
{
public static void main(String[] args)
{

String str = "Din Ami radizs anju ";
Pattern p = Pattern.compile("[a-zA-Z]+");
Matcher m = p.matcher(str);
while(m.find())
{
int start = m.start();
int end = m.end();

System.out.println("Start --" +start);
System.out.println("end --" +end);
}
}
}

o/p --

Start --0 //line 1
end --3 // line2
Start --4 //line 3
end --7 //line 4
Start --8 //line 5
end --14 //line 6
Start --15 //line 7
end --19 //line 8

What i am here doing is searching for a-z and A-Z pattern

line 1 starts looking for the patterns
line 2 found 'A' at position 3
line 3 starts again looking for the patterns
line 4 found the 'a'
line 5 starts again looking
line 6 not bale to get, it should print 12 but prinitng 14
line 7 and line 8 not getting.

in this prog then where is z searched and found
Can anyone explain me please...

Source is my code

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mport java.util.regex.*;
import java.io.*;

public class Regexdemo
{
public static void main(String[] args)
{

String str = "Din Ami radizs anju ";
Pattern p = Pattern.compile("[a-zA-Z]");
Matcher m = p.matcher(str);
while(m.find())
{
int start = m.start();
int end = m.end();

System.out.println("Start --" +start);
System.out.println("end --" +end);
}
}
}

When i removed '+' sign output is different
Start --0
end --1
Start --1
end --2
Start --2
end --3
Start --4
end --5
Start --5
end --6
Start --6
end --7
Start --8
end --9
Start --9
end --10
Start --10
end --11
Start --11
end --12
Start --12
end --13
Start --13
end --14
Start --15
end --16
Start --16
end --17
Start --17
end --18
Start --18
end --19

Can someone please explain this ...
 
sridhar row
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinesh, print m.group() between your print statements and see the output. You will understand why its doing what its doing.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sridar,

Can you explain me the difference between + and * greedy auantifiers.
Please.....
 
sridhar row
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
* means zero or more occurrences

+ means one or more.

Do you have K & B book? If you do read pages 476,477. If you still don't understand let me know i will try to explain.
 
Sabber bhatia
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As i understand

just start counting your letter from 0,so it will go to 19
otherwise write on your page and write number above this
I am not writting beacue not possile here to write.
your input was
Din Ani radizs anju

Hope this will help
Thanks
Himani Ahuja
 
Mamadou Touré
Ranch Hand
Posts: 189
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dinesh,

When you say [a-zA-Z]+ , this means, find 1 or more letters in a row; (I insist in IN A ROW) so "Din Ami radizs anju" will give 4 groups (Din, Ami, radizs and anju"

but when you say [a-zA-Z] (without the "+" at the end), that means find every single letter, (I insiste in SINGLE) so in this case, the groups will be represented by every single letter ( D,i,n, A,m,i,r,a,d,i,z,s,a,n,j and u)

hope this will help
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about when i do [a-z]* ???
please explain...
 
Mustafa Musaji
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dinesh Tahiliani:
What about when i do [a-z]* ???
please explain...


That means find zero or more occurences. So in this case you are looking for ZERO OR MORE occurances of letters a to z.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic