This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Exclude a pattern from regex

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, if I have:


and want to exclude :


I tried (pattern &! exclusionPattern) and (pattern &^ exclusionPattern) but it doesn't work.



How to exclude a pattern from matching?
 
Sheriff
Posts: 6268
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:

(?!s2)(s1)

That is,

(?!excluded pattern)(pattern)
 
Sheriff
Posts: 21804
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An exclamation mark doesn't mean negation (or anything else), except when using it in a negative lookahead or lookbehind. What Knute posted is an example of a negative lookahead. It means "at this point I don't want s2; at this point I want s1".
 
Nesamani Raman
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Knute, many thanks for your reply.

@Rob, what are the other ways in which I can accomplish the exclusion of a pattern?
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here, Negative Lookahead can be used:
(?!) Negative look ahead- Specifies a group that cannot match after the main expression (i.e. if it matches, then the result is discarded)

Try below expression

^(?!s2)(s1)

So, Here s2 will be excluded
 
Rob Spoor
Sheriff
Posts: 21804
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nesamani Raman wrote:Hey Knute, many thanks for your reply.

@Rob, what are the other ways in which I can accomplish the exclusion of a pattern?


Why do you need other ways? If any are possible, they will be too complex and not be readable.
 
Nesamani Raman
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your kind reply, Rob.

I wanted to know if I have multiple patterns to be excluded, how to approach it?

Should I first combine all the exclusion patterns using "|" and then use or is there an efficient/simpler way? I am not sure if I my approach is correct. Please guide.
 
lowercase baba
Posts: 12766
51
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
personally, I wouldn't try and write a monolithic expression that covers all the bases.  Your requirement are bound to change over time, and if you have a single expression, it gets EXTREMELY hard to adjust it later. It's almost impossible to adjust it without breaking something else.

I would instead consider writing many small expressions, each contained in their own boolean method, then aggregate those...



Now it's much easier to swap in and out various conditions as your requirements change.
 
Rob Spoor
Sheriff
Posts: 21804
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nesamani Raman wrote:Thanks for your kind reply, Rob.

I wanted to know if I have multiple patterns to be excluded, how to approach it?

Should I first combine all the exclusion patterns using "|" and then use or is there an efficient/simpler way? I am not sure if I my approach is correct. Please guide.


You could use (?!s2|s3|s4)s1, or you could use (?!s2)(?!s3)(?!s4)s1. They both do the same, but performance may become a factor.


@Fred: with constant strings concatenated you can get the same readability. For instance:
 
Nesamani Raman
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"You could use (?!s2|s3|s4)s1, or you could use (?!s2)(?!s3)(?!s4)s1. They both do the same, but performance may become a factor. "

Which would be faster in which case? Can you please elucidate?

Looking forward to your kind response.

Thanks!
 
Rob Spoor
Sheriff
Posts: 21804
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know, you would have to try both and measure how they perform. That doesn't mean run the code once; for proper measurements you need to have your code run several thousands of times. JMH can help with that.
 
fred rosenberger
lowercase baba
Posts: 12766
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't worry about performance unless you have a documented spec on what the performance should be.  "As fast as possible" is NOT a valid spec.  "less than 10 milliseconds" is.  

You should almost always focus on readability and maintenance over speed.  After all, if you save 1 millisecond by optimizing code, but then spend 20 hour debugging it in the future, you're probably going to be at a net loss - especially since a user won't ever notice the savings.
 
You got style baby! More than this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!