• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex word containing a substring?  RSS feed

 
Mike Lonsky
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to use regex to find a word containing the substring "cat." I'm not sure why this isn't working? String match = "^ cat$";
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because that regex is looking for a line containing only the letters 'c', 'a' and 't'. You need to tell it you will accept other characters either side of that.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^ = beginning of line
" cat" is a literal
$ = end of line.

So you are actually looking for a line that has exactly one space followed by "cat" and then ends. There can be no other characters.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!