• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pattern/Matcher - how to use "and" statement?  RSS feed

 
Adrian Burlington
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I'm trying to write a pattern where a line should begin with a digit AND end with a digit. The problem that I face is that both conditions should occur together (starts with and ends with).

Can any one point how this should work? for code simplicity I need to write one pattern (not to use startsWith or endsWith)


Thanks
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to work with regular expressions you need to spend some time here - http://www.regular-expressions.info/ .

If you are going to use Matcher.matches() then there is no need for the '^' or the '$' since they are implicit when using matches() and all you need is -

 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd change that into "\\d(.*\\d)?", to also allow lines with only one single digit. Those too meet the requirements of both starting and ending with a digit - they just happen to be the same.
 
Adrian Burlington
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you both! it works.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!