| Author |
Question regex
|
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
|
|
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
|
Thanks<br />Dinesh
|
 |
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
|
|
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
Joined: Jan 16, 2008
Posts: 162
|
|
|
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
Joined: Aug 06, 2007
Posts: 486
|
|
Sridar, Can you explain me the difference between + and * greedy auantifiers. Please.....
|
 |
sridhar row
Ranch Hand
Joined: Jan 16, 2008
Posts: 162
|
|
* 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
Joined: Jun 27, 2006
Posts: 59
|
|
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
Joined: Dec 27, 2007
Posts: 189
|
|
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
|
SCJP 5 (76%)
SCWCD 5 (86%)
SCBCD 5(70%)
--------------------
"The greatest glory in living lies not in never falling, but in raising every time we fall.".. Nelson Mandela
|
 |
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
|
|
What about when i do [a-z]* ??? please explain...
|
 |
Mustafa Musaji
Ranch Hand
Joined: May 03, 2008
Posts: 52
|
|
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.
|
SCJP 5.0 - Passed
|
 |
 |
|
|
subject: Question regex
|
|
|