View Full Version : Is there an easy way to convert $$ tags to [tex][/tex]?
Simfish
Dec23-10, 07:58 PM
I know that there's probably a way to do this with regexp, but I don't know regexp (yet). So does anyone have any tips on how to create the regexp expression for it? (and what other ways are there?)
davee123
Dec24-10, 05:11 PM
I know that there's probably a way to do this with regexp, but I don't know regexp (yet). So does anyone have any tips on how to create the regexp expression for it? (and what other ways are there?)
What language are you using? Do you have an example of a string you'd like to convert from and what you'd like to convert it to?
DaveE
Simfish
Dec24-10, 05:19 PM
regexp would be sufficient enough (regexp works with lots of languages). Just a search-replace string that I could use for a program that uses regexp.
So convert $x^2$ into [tex] x^2[/ tex]
perl -e 'undef $/; $_ = <>; @a = split /\$/; while (@a) { print(shift @a); print("".(shift @a)."") if (@a); }'
(This will choke if there are stray dollar signs.)
A pure regexp solution is likely to run into greedy-match problems if there is more than one $$ pair in the string.
EDIT: It wants to interpret my TEX tags as actual tex tags. Hit quote if you want to see the actual source.
davee123
Dec25-10, 07:47 PM
Yeah, Perl-wise, I think I'd do:
$string =~ s/\$([^$]*)\$/\$1\[\/xet\]/gs;
As pointed out, this won't always work if there are any extra $ signs floating around. So if you got a string of:
I paid $3 to Tim to get the answer $x^2$, but Bob only charged $2.
That obviously wouldn't work. It would change it into:
I paid [xet]3 to Tim to get the answer x^2, but Bob only charged 2.
But you REALLY wanted:
I paid $3 to Tim to get the answer x^2, but Bob only charged $2.
If there are rules about what can go inside the $$, you can write that into the regexp to make it more fool-proof, but I'm not sure what the rules might be. For instance, if there is no whitespace allowed within the $$, you could use the regexp:
$string =~ s/\$([^$\s]*)\$/\[xet\]$1\[\/xet\]/gs;
And that'd work on the above string.
Fought with IP board code about recognizing tags-- just pretend that "xet" is "tex", in the above.
DaveE
Yeah, Perl-wise, I think I'd do:
$string =~ s/\$([^$]*)\$/\[xet\]$1\[\/xet\]/gs;
As pointed out, this won't always work if there are any extra $ signs floating around.
I think you can get around this by using perl's ridiculous ?<! operator ("zero-length negative look-behind"!), something like:
$string =~ s/(?<!\\)\$([^\$]*[^\\])\$/\[xet\]$1\[\/xet\]/gs;
This should refuse to match $s that are preceded by a \, either inside or outside tex areas. So like you'd be able to write "I gave steve \$3" and it would safely understand this is not a tex delimiter.
I was worried greedy matching would mess you up (ie "I think $x^2$ is a better equation than $2^x$" and it tries to match "is a better equation than" as the tex area) but when I tested this actually wasn't a problem! So there's your solution. Although I'd take advantage of perl -p:
perl -pe 's/(?<!\\)\$([^\$]*[^\\])\$/\[xet\]$1\[\/xet\]/gs;'
Simfish: Try this substitute (s) command.
:set magic
:s;$\([^$]*\)\$;\1;g
If you want it to change only the first occurrence of $$ in the line, remove the "g" character. (The colon shown above just represents the command mode prompt, and is not part of the commands.)
To all of you fighting tags recognition by forum software - try tags.
Thanks for all the replies, everyone! :) Based on the input, I've created my own regexp expression (for the JGSoft software that I use). Which is basically this:
search: \$([^$]*)\$
replace: $1
===
It actually might be helpful to put sample regexp expressions on the PF Help threads about LaTeX (since it makes it much easier for people to convert their tags from their documents to the LaTeX demanded by PF).
Borek, thanks, is that documented somewhere?
Thanks for all the replies, everyone! :) Based on the input, I've created my own regexp expression (for the JGSoft software that I use). Which is basically this:
search: \$([^$]*)\$
replace: $1
===
It actually might be helpful to put sample regexp expressions on the PF Help threads about LaTeX (since it makes it much easier for people to convert their tags from their documents to the LaTeX demanded by PF).
Or maybe just post a converter script somewhere.
Actually, I'm pretty sure you could do this particular regexp in Javascript. If I submit a "convert $tex$ to [xet] tags" javascript to will physicsforums post it somewhere?
Borek, thanks, is that documented somewhere?
http://www.vbulletin.com/forum/misc.php?do=bbcode
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.