Skip to main content
gamedev.net
🔒 Locked

Regular Expression

Started by Mulligan 13 replies 1.4k views
Original Post
Mulligan
Mulligan
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?
ju2wheels
ju2wheels
Yes you are right, regular expressions can make that very easy. Ive only used Perl regular expressions but I believe boost allows for the use of regular expressions using C++.

You can then use a search/replace regular expression to do what you want.
Enselic
Enselic
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]
ToohrVyk
ToohrVyk
nicollet@clipper /users/04/info/nicollet> echo "<box round red|Note>This is some text, wow|" | sed -e 's/<\([^ |]*\)\([^|]*|[^>]*>[^|]*\)|/<\1\2<\/\1>/g'<box round red|Note>This is some text, wow</box>
Deyja
Deyja
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.

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.
Mulligan
Mulligan
Thanks! Ill give those a whirl
Zahlman
Zahlman
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))
Nitage
Nitage
VS2003 and VS2005 have a regex function built into their find/replace dialogue.
Enselic
Enselic
Quote:
Original post by Nitage
VS2003 and VS2005 have a regex function built into their find/replace dialogue.

Yeah, but afaik it is not capable of this type of search and replace?
ToohrVyk
ToohrVyk
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.

ToohrVyk
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 ?
Mulligan
Mulligan
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.