• Post Reply Bookmark Topic Watch Topic
  • New Topic

String negation in regular expression  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I was wondering how to negate a string (!!! Not a single character). For example I need to write an expression that evaluates to true if the string does NOT contain for example 'foo'. There is not something like !(foo).

Any ideas,

Greetz,

Ivo Willemsen
[ December 26, 2006: Message edited by: Bear Bibeault ]
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Several possibilities:



That last one uses negative lookahead, which isn't well-explained in the Java API, but you can learn more here.
[ December 26, 2006: Message edited by: Jim Yingst ]
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String class also contains some regex convenience methods, so you can also use...



Henry
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. Too bad they only do this for matches() but not find(), since the latter is closer to the intent here, I think. Using matches() requires extra .* several places, which I find mildly annoying (mostly because I forgot them in my first post, now fixed).
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!