0

For instance..

Sample Text: <tag>AandBandCandD</tag>

I need to match everything that occurs between tag> and </tag excluding any instances of "and" so the result is:

"ABCD"

5
  • My sample text broke because of the tags. Should be "tag>AandBandCandD</tag" Commented Aug 13, 2012 at 18:50
  • fixed that up, your question should read as you intended, please check Commented Aug 13, 2012 at 18:51
  • What language are you using? Can you post what you have tried? Commented Aug 13, 2012 at 18:52
  • It's being used in an application UI. I have zero programming options. Currently using somegthing like "tag>(.*?)</tag" but don't know how to exclude the "and"s Commented Aug 13, 2012 at 18:54
  • @user1596224: Without knowing what regular expression engine is used for the application UI's we can only guess. There is no "standard" regular expression language. Every language has its own implementation and they are all different. It could be that what you are trying to do is impossible. Commented Aug 13, 2012 at 18:56

2 Answers 2

2

I'd do this in two steps:

  • First extract the contents of the tag.
  • Next replace all occurrences of and with the empty string.

Regular expressions are not the right tool for either step:

  • The first step can be better achieved using an HTML parser.
  • The second step doesn't need the complexity of regular expressions. A simple string replace will work.

Update

If you are forced to use a single regular expression, then it's probably not possible.

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately the field only takes regular expressions. Again this is an input field of a 3rd party application. I have 0 development options.
I think that may be the case. Thanks :)
1

This is something you can try, anyway.

(?<=<tag>)(?:((?!and).)+(?:and)?)*(?=</tag>)

Broken down for readability:

(?<=
  <tag>
)
(?:
  (
    (?!
      and
    )
    .
  )+
  (?:and)?
)*
(?=
  </tag>
)

I'm pretty sure this would produce a number of matches equal to the text between the ands. But without knowing the exact regex language in question, there's a ton of things that could break this by being just a little different.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.