RegEx nearest

Posted on 2012-08-27
Last Modified: 2012-08-28
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)
Question by:chambarish
    LVL 34

    Expert Comment

    by:Terry Woods
    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.

    Author Comment

    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/.
    LVL 18

    Expert Comment

    by:Gary Davis
    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
    LVL 34

    Accepted Solution

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now