🔒 Locked
Regular Expression
Advertisement
Original Post
I think I could solve this problem with regular expressions, but I'm not sure how... Basically, I have several thousand statements of the format: <box round red|Note>This is some text, wow| Where the only variation is in the text between > and |. There is guarenteed to be a "|" following "<box round red|Note>". In a best case scenario, I would use visual studio to do a find and replace in files that would take the above expression, and convert the trailing "|" to a "</box>". The only thing that complicates this is that there are thousands of other "|" characters, which rules out a straight replacement for the character. Example <box round red|Note>This is some text, wow| becomes <box round red|Note>This is some text, wow</box> Any suggestions on how to do this?
To find the lines matching <text|text>text|, use this regex:
<[^|]*|[^>]*>[^|]*|
< then any characters but |, then |, then any characters but >, then >, then any characters but |, then |
Then, (if they are not very very many), you could manually replace them since you will have a nice double-clickable list of all of these occourances?
Edit: D'oh, you've got thousands. I guess you could hack together a quick program/script to find all these lines and replace them.
[Edited by - Enselic on July 31, 2006 3:49:05 PM]
<[^|]*|[^>]*>[^|]*|
< then any characters but |, then |, then any characters but >, then >, then any characters but |, then |
Then, (if they are not very very many), you could manually replace them since you will have a nice double-clickable list of all of these occourances?
Edit: D'oh, you've got thousands. I guess you could hack together a quick program/script to find all these lines and replace them.
[Edited by - Enselic on July 31, 2006 3:49:05 PM]
Toohr wins; if the OP happens to be running linux. (Though, of course, you'd want to pipe from a file, and back to it, instead of right to the command line.)
If not, this can be whipped together pretty quickly using boost::regex and a couple of fstreams.
Psuedo-codified.
P.S, my regex is non-escaped. :/ And requires atleast 1 character for the 'tag name' word.
If not, this can be whipped together pretty quickly using boost::regex and a couple of fstreams.
Psuedo-codified.
regex = "(<([^|]+)|[^>]*>[^|]*)|"fstream in = open(in_filename);fstream out = open(out_filename);std::string line = in.get_line();while( ! end of file ){ regex.match(line); out << regex.group(0) << "</" << regex.group(1) << ">"; line = in.get_line();}P.S, my regex is non-escaped. :/ And requires atleast 1 character for the 'tag name' word.
Or you could do it in a language with better support for regexes, such as Perl or (my preference) Python:
import reregex = re.compile(r"(<([^|]+)|[^>]*>[^|]*)|")out = file(out_filename)for line in file(in_filename): # I think this is right... out.write(regex.sub(lambda match: match.group(0) + "</" + match.group(1) + ">", line))
Quote:
Original post by Enselic
Yeah, but afaik it is not capable of this type of search and replace?
It is. So are many other text editors (Crimson Editor, for instance). You have to check the regular expression tickbox, and have it replace
<\([^ |]*\)\([^|]*|[^>]*>[^|]*\)|
by
<\1\2</\1>
Disclaimer: you might have to escape either the / or the | for this to work.
Quote:
Original post by ToohrVyk Quote:
Original post by Anonymous Poster
echo '<box round red|Note>This is some text, wow|' | sed 's%|$%</box>%'
Well, can you assume that all the tags to be closed are of type box ? Can there be other tags, such as span, which can't be closed this way ?
Its not HTML, its Doku wiki syntax for a box extension, so its always going to end with a </box>
Thanks for all the replies, very interesting
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement