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!

Reg expression help please... 2

Status
Not open for further replies.

nickroper

Programmer
Jun 1, 2004
24
GB
Hi,

I have a regular expression that converts newline characters to '<BR>'. I need to modify this so that it only converts the newline characters if they are not preceeded by certain other strings ('table>', 'td>' and 'tr>')

So, examples would be as follows:

original string:

'line1\n line2\n line3'

would be converted to:

'line1<BR> line2<BR> line3'

Whereas:

'<table>\n<tr>\<td>line1\nline2\nline3</td>\n</tr>\n</table>'

would be converted to:

'<table>\n<tr>\<td>line1<BR>line2<BR>line3</td>\n</tr>\n</table>'

My existing expression is:

$foo =~ s/\n/<BR>/g;

which converts ALL occurrences of \n. If any regex gurus out there can let me know the trick to ignore \n preceeded by 'table>', 'td>' and 'tr>' (I've left the opening bracket off in order to catch both openng and closing tags) then I'd be extremely grateful.

Cheers,

Nick Roper
 
Cheers Trojan!

No bike = misery :-(

Nick

try using single quotes around your string - not double speech marks


Kind Regards
Duncan
 
Just noticed that my initial string was double-quoted so that meta-characters are expanded. Should this make a difference as far as the regex is concerned ?

Nick
 
OK, I've got it.

I modified the expression to un-escape the '\' in front of the 'n'. Your expression worked perfectly with the examples that I gave - which were single quoted strings, but I didn't mention that $foo is actually read from a file and that the \n would be a real newline sequence rather that a literal '\n'

Thanks again for the help - now I can go for that pint of Butcombe...

Cheers,

Nick
 
Duncan

Off-topic I know, but how did you post the picture? I visit the OO design forum a lot, and sometimes a diagram is worth a thousand words, and certainly much easier than trying to 'draw' one with |+><- characters inside
Code:
 tags.

Nice bike, BTW. I hadn't ridden one for about 8 years, but rented some scooters on holiday earlier this year. I'd forgotten how much fun it was. Hmmm, maybe next spring...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top