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

Getting the right order with Regular Expressions

Status
Not open for further replies.

RedRobotHero

Technical User
May 2, 2003
94
CN
I'm making a CGI script that will convert another file format into HTML with style tags.

In this format, (Xtags) there's two types of tags I want to use, one of the format
Code:
<@[a-zA-Z]+>
and one of the format
Code:
@[a-zA-Z]+:
.

I want to deal with these codes in the order in which they appear from left to right. But I have to use different code for the two cases.

The approach I've thought to use is this:

Code:
while ((/<@[a-zA-Z]+>/)&&($c1=1)||
       (/@[a-zA-Z]+:/)&&($c2=1)) {

 if ($c1==1) {
    deal with the tag and replace it with new tag.
  };

 if ($c2==1) {
    deal with the other tag and replace it with new tag.
  };

 $c1=0;
 $c2=0;
}


Is this really the best approach? Or is there a better way for me to deal with this?
 
Well, that's the thing. I tried it, and I did something wrong, because it doesn't work. And before I go to the trouble of debugging it as it stands, I wanted to know if there is a tried-and-tested method that I should be using instead.
 
read in the file to a array

foreach $line (@array){
$line=~s/<[a-z] +>//gi;
$line=~s/<[a-z] +\/>//gi;
}

I am not quite sure how you identifing the tags
but this is the perl substitution method the
g mean global
i case insensative

hope this helps
 
If I just read the lines in order, there could still be more than one tag per line. And I need to know the order, because what I replace a tag with depends on what tag preceded it.
 
So you are trying to match tags, and you need to keep track of the preceding tag, maybe this:
Code:
while ( m/<?           #Angle bracket, or not
          @[a-zA-Z]+   #common between your two regexes
          (>|:)        #other bracket, or semicolon
         /gx           #the g will make give us one
                       #match per loop, the match is
                       #avaiable via the $& variable. 
      ){

    $matched_tag = $&;
    $tag_type    = ($1 eq '>') ? &quot;type one&quot; : &quot;type two&quot;;

    if($last_tag eq $matched_tag){
      .. do stuff ..
    }
    else{
      .. do something else ..
    }

    $last_tag    = $matched_tag;

}
Hope this helps...

--jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top