Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Doubt about Regex and Quantifiers

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I need some help!
(code below from Sun´s tutorial)



So I run this code.



My question is: why only in the last string (aabbaba) there´s a match of zero length and others (a, ab, aaba) don´t have??
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My question is: why only in the last string (aabbaba) there´s a match of zero length and others (a, ab, aaba) don´t have??



Take a look at your regex. The last case has a different regex -- that can match a zero length string.

Henry
 
Nya Iwa
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry!
Could you explain to me why the last case "can match a zero length string" and the the others can't? I know the regex is different, but why when I try a? I can match a zero length string and when I try aa? I can't match it?

I'm so confused! Why this output?
When I try by myself (on paper) I don't get this output!
The relationship between quantifiers, zero-length string and matcher.find() isn't very clear to me..
I don´t know exactly where I'm doing wrong..

Could you please explain to me step by step following the source code how the third case works?
It would be of a great help!!

 
Henry Wong
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Could you explain to me why the last case "can match a zero length string" and the the others can't? I know the regex is different, but why when I try a? I can match a zero length string and when I try aa? I can't match it?



The "?" means zero or one of the previous character. So....

aa? ... matches a letter "a", followed by zero or one "a"s.... basically, it can match "a" or "aa".

a? ... matches zero or one "a"s.... basically, it can match nothing or "a".

Henry
 
Henry Wong
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When I try by myself (on paper) I don't get this output!
The relationship between quantifiers, zero-length string and matcher.find() isn't very clear to me..
I don´t know exactly where I'm doing wrong..



Well, in order to tell you what you are doing wrong, we need to know what you are doing. Please explain to us what you put on paper...

Henry
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic