Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex question

 
Maren Fisher
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following question is from "A Programmer's Guide to Java SCJP Certification: A Comprehensive Primer. Third Edition" By Khalid A. Mughal and Rolf W. Rasmussen. It is question 12.14


The answer given is the following (which I also confirmed by running it locally).


However, I don't understand WHY it works. In the regex expression:


doesn't the .? mean match any character 0 or 1 times. So given the input "JAVA JaVa java jaVA" why doesn't it do the following:
[Jj] match the first J
.? match the A (any character)
[Aa] - fail match because the next character is the 'V'

I don't understand why the .? doesn't match the A

Thank you
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ".?" could match the A - but then the overall match would fail, as you correctly point out. But it can also match nothing (the "0 times" part) - in which case the rest of expression will also match, for an overall success. A partial match failure causes the regexp engine to backtrack and try other possibilities.
 
Maren Fisher
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lester Burnham wrote:A partial match failure causes the regexp engine to backtrack and try other possibilities.


Hmm..okay, that was the part I was missing. Thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic