Replacing all instances of newlines not inside tags
I have a body of text with newlines in it, and I would like to开发者_开发百科 replace all instances of newlines that are not inside certain tags with <br />
s. The tags are [something][~something]
, where something
could be anything.
Sorry for not including what I have already tried, but I have no idea how to get a regex to match something not inside something else. Is that even possible?
While remembering that you should not parse XML/HTML with a regex and this is an instance of that:
#!/usr/bin/perl
#
$in = "foo\nbar\nbaz[something]else\nis\n[~something]\nHappening\n[something]else\nis\n[~something]here\n";
$out = "";
while ($in =~ s/^(.*?)(\[something\].*?\[~something\])//s)
{
my($before,$during) = ($1,$2);
$before =~ s/\n/<br \/>/g;
$out .= $before.$during;
}
$in =~ s/\n/<br \/>/g;
$out .= $in;
print $out;
Yes, not in your language of choice, but the concept (parsing the data in stages stuff before your tags and stuff inside your tags while deleting the processed data) should be portable.
精彩评论