0

... AA BB sysodufsoufdds BB AA ...

Where AA,BB can be arbitary consecutive string with no space in it.

But I want to get the outest pair:AA

More examples:

Input:

a HH CC abc CC HH c

Output:

HH

Input:

x YYYY j DD GG DD hsu DD GG DD k YYYY o

Output:

YYYY

To make my question more general,how to match a specific tag in html with regular expression?I've seen various posts discussing about this,but none of them give a answer by regex.Related questions are: I'm looking for a regular expression to remove a given (x)HTML tag from a string

3
  • for input: "x YYYY DD GG hsu GG DD YYYY", is YYYY a valid answer? Commented Jan 13, 2010 at 12:23
  • The second example doesn't follow the pattern you start with. The second example is AA str1 BB str2 BB str3 AA (i.e. there is more than white space between the paired strings). Commented Jan 13, 2010 at 12:53
  • For the example you provided,the output is AA Commented Jan 13, 2010 at 12:59

2 Answers 2

1
\b(\w{2,})\b.*\b\1\b

will match everything from the first series of consecutive characters until its repetition. Backreference \1 will contain the pattern that was matched (e. g. AA, HH or YYYY in your examples).

The \bs are necessary to enforce word boundaries.

EDIT: Oh. I just noticed that you want to do something else entirely, namely remove HTML tags from a string/file. Don't use regexes for that. I won't quote the article that everyone else always quotes when someone asks a question like this, but the problem (in a nutshell) is that HTML is not regular, and trying to use regexes here is just asking for trouble. That's the reason why nobody (in their right mind) uses regular expressions to "parse" HTML - they use a parser.

That said, I have used regexes to extract data from well-formed XML sources where I knew the structure exactly and knew that the tags I'm interested in would never be nested etc. - but recursion with regular expressions is just horribly complicated if it works at all (C# and Perl have some support for that, but it's incredibly hairy).

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

1 Comment

+1 for an excellent answer, and a heartfelt "thank you" for not linking to The Rant.
0

I think you need back references here. Something like (trying to avoid specifics of any regex language):

(\w+) \w* (\w+) \w+ \1 \w* \2

With the first capture being you result.

I've assumed single spaces separating the strings to keep it clearer, you probably need to allow for arbitrary whitespace with \s+, and \w (identifier characters: roughly [a-zA-Z9-0_]) is the right match for the strings.

3 Comments

To make my question more general,how to match a specific tag in html with regular expression?I've seen various posts discussing about this,but none of them give a answer by regex.Related questions are:stackoverflow.com/questions/116403/…
Regex is, generally, the wrong approach unless the context is substantively constrained (e.g. you control the source). Better to use a parser (which could be an XML parser if you know it is XHTML).
Suppose I'm dealing with a html like stuff,but not exactly html.Then the DOM parser won't work.

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.