views:

269

answers:

3

... 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: http://stackoverflow.com/questions/116403/im-looking-for-a-regular-expression-to-remove-a-given-xhtml-tag-from-a-string

A: 

Hi

I can't figure out what you want from your example. I won't guess, I suggest you edit your question and clarify. You might:

  1. Give some more example inputs and outputs.
  2. Clarify whether AA that you want is the A from the beginning of the string and the A from the end, or some other pair of As from the string.
  3. Let us know why, if AABB is a string with no space in it you write your example with a space.
  4. Clarify whether or not the text between, in your example BB and BB may, or may not contain AA or BB.

Help us to help you.

Yes, I know this is a comment not an answer, but it's easier to write it as an answer.

Regards

Mark

High Performance Mark
you're right: this should be a comment
Rubens Farias
I've just provided more examples,should be clarified.
A: 

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.

Richard
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:http://stackoverflow.com/questions/116403/im-looking-for-a-regular-expression-to-remove-a-given-xhtml-tag-from-a-string
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).
Richard
Suppose I'm dealing with a html like stuff,but not exactly html.Then the DOM parser won't work.
+1  A: 
\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).

Tim Pietzcker
+1 for an excellent answer, and a heartfelt "thank you" for not linking to The Rant.
Alan Moore