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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression issue...

Status
Not open for further replies.

newbie1006

Technical User
Jun 10, 2011
9
US
I have a script that would check a xml file and find me strings that are between tags <Register> ...</Register>

script is :
while (my $line = <FILE>)
{
if ($line =~ m/<Register>/)
{
my $nextline =<FILE>;

if ($nextline =~ /^(.*)]*<\/Register/)
{

my $string = $1;


push(@register_list, $string);


}

else
{
print "You have a problem \n";

}
}
}

which is giving me the following

SG_u_EO_RX_FI_RD_WR_PT_OET[4:0]
GG_RR_AA_FFER_ASD_u_aSD_DAS_B[99999999]
GG_RR_AA_u_aSD_DAS_C[25:0]

what can i do so i get only this

SG_u_EO_RX_FI_RD_WR_PT_OET
GG_RR_AA_FFER_ASD_u_aSD_DAS_B
GG_RR_AA_u_aSD_DAS_C
 
Not knowing how your xml file is put together, your code as written looks problematic. However, to keep things moving along and answer the question as asked: A couple of options

Trim off the trailing []'s after you've found the string
Code:
my $string = $1;
$string =~ s/\[.+// ;
push(@register_list, $string);
This will look for a "[" and everything after it, then remove it.

Trim off the []'s as you're finding themp
Code:
if ($str =~ /^(.*)\[[^[]+\]<\/Register/) {
   push(@register_list, $1) ;
};

One item that is (arguably) not an option - indent your code.
 
Reviewing my second example, this may be better
Code:
if ($str =~ /^(.*)\[[^\]]*\]<\/Register/) {
   push(@register_list, $1) ;
};

Breaking this down:
\[ ' find a left square bracket
[^\]]* ' everything that is not a right square bracket
\] ' find a right square bracket
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top