2

What is the regular expression that can match the following 2 strings.

Hi<Dog>Hi and <Dog> in a given text.

Update:

What regex will match this one?

<FONT FACE="Verdana" SIZE="16" COLOR="#0B333C" LETTERSPACING="0" KERNING="0">If you access the web site click the link below:<FONT SIZE="12"></FONT></FONT>

<FONT.*?<\/FONT> matches only till the first </FONT>

VLAZ
  • 26,331
  • 9
  • 49
  • 67
java_geek
  • 17,585
  • 30
  • 91
  • 113
  • 4
    There are *lots* of regular expressions which would match those strings. ".*" would do it for example... as well as matching everything else, too. Please give more details about what you're trying to do. – Jon Skeet Aug 17 '10 at 09:20
  • 1
    `abab|` should match both of those ;) – Delan Azabani Aug 17 '10 at 09:26
  • possible duplicate of [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) –  Aug 17 '10 at 14:37

4 Answers4

3

The pattern ^([a-z]*)<[A-Z]*>\1$ will match these strings (as seen on rubular.com):

ab<XYZ>ab
<XYZ>
bleh<FOO>bleh
<>

It will not match these:

ab<XYZ>de
x<XYZ>y
FOO<foo>FOO

That is, the pattern is something like

tag<CONTENT>tag

The same tag appears for both the "prefix" and the "suffix". Tag consists of zero or more lowercase letters. Content consists of zero or more uppercase letters. The prefix part is matched and captured by group 1, and then a backreference \1 is used to match that string again for the suffix.

The […] is a character class. Something like [aeiou] matches one of any of the lowercase vowels. [^…] is a negated character class. [^aeiou] matches one of anything but the lowercase vowels.

As a Java string literal, the pattern is "^([a-z]*)<[A-Z]*>\\1$".

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
1

Not sure what you try to do, but this captures all the possibly relevant groups:

([a-z]+)?(<[A-Z]+>)([a-z]+)?

Good Luck!

FK82
  • 4,907
  • 4
  • 29
  • 42
0

Off the cuff I think it should be something like (.*)<XYZ>\1

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
0

Use http://www.weitz.de/regex-coach/ to check whether a regular expression matches a string

That's the only advice I can give you with the info you're giving us.

KristofMols
  • 3,487
  • 2
  • 38
  • 48