Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a matching regular for <hi>hello</hi> ?

Status
Not open for further replies.

shlomyb

Programmer
Nov 14, 2006
32
IL
Hi

what is the matching regular expration for :

<hi>hello</hi>

Is it ---> $line =~ m!<[^>]*>(\S+)</[^>]*>!)


Thanks.
 
Your code seems to work, but it will also catch non-matching tags, try the following, which will only match matching tags, and case-insensitive.

$line ="<AB>hello</CD>";
$line1="<Hey>Good Day</HEY>";
if($line =~ m'<(\w+)>.*</\1>'i){printf("A1:%s\n",$1);}
if($line1 =~ m'<(\w+)>.*</\1>'i){printf("A2:%s\n",$1);}
if($line =~ m!<[^>]*>(\S+)</[^>]*>!){printf("B1:%s\n",$line);}
if($line1 =~ m!<[^>]*>(\S+)</[^>]*>!){printf("B2:%s\n",$line1);}

A2:Hey
B1:<AB>hello</CD>

Tool completed successfully
 
Are you wanting to match all strings with the format <text>othertext</text>, regardless of what the actual text is, as long as there is something there?

If so, you're pretty close.

Code:
$line =~ m!<[^>]*>(\S+)</[^>]*>!

The only obvious problem I see is that <[^>]*> would match an empty bracket <>, because * means zero or more.

Also, you are excluding whitespace from the middle part of the string, but not the two parts within brackets. This RE would match <some text>othertext</some text>, but not <sometext>other text</sometext> Is this really what you want?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top