• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

RegEx nearest

I have this text
(Objectid < 200 OR ObjectID > 600) and (test or best) W/5 AND (apple OR 10a) AND (Objectid < 100 OR ObjectID > 500)

I want to get nearest string with in() having And Or in it nearest W/digit both left and right of W/5.

In the above example I should get (test or best) (apple OR 10a)
  • 2
1 Solution
Terry WoodsIT GuruCommented:
Building on kaufmed's solution to your previous question, this seems to work:

Open in new window

I used the ignore case option for testing it.
chambarishAuthor Commented:
Thanks, I have one concerns with this.
I should get only the group if it is adjacent to W/
For Ex:
(Objectid < 200 OR ObjectID > 600) and test  W/5 AND (apple OR 10a)
If text next to w/ is enclosed with in brackets then only fetch
In the above example it should give me (apple OR 10a) and not (Objectid < 200 OR ObjectID > 600) as it is not present besides w/.
Gary DavisDir Internet SvcsCommented:
How about:

(\([^()]+\b(?:AND|OR)\b[^()]+\))? W/\d+ AND (\([^()]+\b(?:AND|OR)\b[^()]+\))?

Open in new window

This is like the above solution but requires a space between the close paren and the W5 and that the W5 be followed by AND and the open paren of the next segment.

Gary Davis
Terry WoodsIT GuruCommented:
Gary, that pattern doesn't seem to work with:
(Objectid < 200 OR ObjectID > 600) and W/5 and test AND (apple OR 10a)

Open in new window

Can you also have "OR" next to the W/number such as in:
(Objectid < 200 OR ObjectID > 600) OR test W/5 OR (apple OR 10a)

Open in new window

If it can be "and" or "or" (or just be directly adjacent to the bracketed text without either) then this seems to work:

Open in new window

If you can only have "and" (or just be directly adjacent) and don't want to allow "or", then use this:

Open in new window


Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now