• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please justify output in Regex Java program code

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have came across one Java program in Regex .

Below is the program code :

Output :

true
>0  <
true
>1  <
true
>2  34<
true
>4  <
true
>5  <
true
>6  <
Doubt : As we all know that The find() method returns true if it gets a match and remembers the start position of the match. If find() returns true, you can call the start() method to get the starting position of the match, and you can call the group() method to get the string that represents the actual bit of source data that was matched. My question is how come ">6 <" is present is the output when the string indexing is till index 5 ?

 
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's simply how regular expressions work. If your pattern can match an empty string, you can find an occurrence at the end of the input, behind the last character.
 
Saxena Vishal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:That's simply how regular expressions work. If your pattern can match an empty string, you can find an occurrence at the end of the input, behind the last character.


Ok  Stephan ! I understood that it matches zero or more characters here and therefore ">6 <" is also included in the output . So considering this concept it should go beyond index '6' i.e 7 , 8 etc because there is no such constraint and it will keep on matching forever ?
 
Saxena Vishal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saxena Vishal wrote:

Stephan van Hulst wrote:That's simply how regular expressions work. If your pattern can match an empty string, you can find an occurrence at the end of the input, behind the last character.


Ok  Stephan ! I understood that it matches zero or more characters here and therefore ">6 <" is also included in the output . So considering this concept it should go beyond index '6' i.e 7 , 8 etc because there is no such constraint and it will keep on matching forever ?



I have understood the concept . Thanks for your valuable answer .
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider your string to match--

---->cells--->
a - 0th location cell
                  b - 1st location cell
                  3 - 2nd location cell
                  4 - 3rd location cell
                  e - 4th location cell
                  f  - 5th location cell

i have shown above how actually the cell number and index differs.
now your regex is ----> "\\d*" which is a digit( 0-9 ) zero times or more.

as you use a greedy quantifier it will find a first match as

-->at index-0 where no any digit is present and as it start at 0(and ends also at 0) with no digit so it gives a output with a 4 white space(/t) showing no any digit accordingly with your print format.here match is "" which is a zero length string
-->next match at index 1 and ends at 1.match is "".
-->next match starts at 2 but ends at 4 and will not backoff( for the match like 34,23,4,3,2,"") because of greedy quantifier being used their.
-->next match at 4 and ends at 4.
-->next match at 5 and ends at 5.
-->next match at 6 and ends at 6.

you can clarify the matches with the above shown string(accordingly with index and cells).

Hope it helps!


Regards,
praveen.
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saxena Vishal wrote:So considering this concept it should go beyond index '6' i.e 7 , 8 etc because there is no such constraint and it will keep on matching forever ?



no their is a constraint how can it go beyond its index(even doesnot make any sense).at 6th index it just starts their and also ends their and theirafter nothing is left to match with regex.
 
Saxena Vishal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

praveen kumaar wrote:

Saxena Vishal wrote:So considering this concept it should go beyond index '6' i.e 7 , 8 etc because there is no such constraint and it will keep on matching forever ?



no their is a constraint how can it go beyond its index(even doesnot make any sense).at 6th index it just starts their and also ends their and theirafter nothing is left to match with regex.


Thanks Praveen for such a valuable information about regex  !
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a cow, Praveen.
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the cow stephan.
 
reply
    Bookmark Topic Watch Topic
  • New Topic