• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reg Expression to Search for Non-matching Pattern  RSS feed

 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to use reg exp to search for, say, a valid IP address from a list of random text excluding those IP addresses of the form 192.168.*.*, 127.*.*.*, 10.*.*.*. Is there any way to use reg expression to specify a non matching pattern? For example, [^192] means a SINGLE character not equals to either 1, 9 or 2. But instead, i want to specify a string of 3 characters not equal to "192". Regular expression does not seem to allow this. Am I right?
 
Antany Vasanth
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alec Lee

You can try grouping the character like ?

Regards,
Antany.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you can't. Character classes cannot contain sequences of characters - they match exactly one character.

Take a look at the API for java.util.Pattern, and check the positive/negative lookahead/lookbehind operators.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Antany Vasanth wrote:Hi Alec Lee

You can try grouping the character like ?

Regards,
Antany.


As Rob already mentioned: that will not work. Even the grouping inside a character class does not work. Your regex, [^(192)], will match any character other than '(', '1', '9', '2' or ')'. As you can see, the '(' and ')' are just matched as normal characters: only outside a character class they're used to group characters.
 
Antany Vasanth
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Piet,

The information is very helpfull.

Antany
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Antany Vasanth wrote:Thanks a lot Piet,

The information is very helpfull.

Antany


You're welcome Antany.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!