CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
A way to check for "matching" characters?

 



Wumbate
New User

May 7, 2008, 10:34 PM

Post #1 of 4 (4482 views)
A way to check for "matching" characters? Can't Post

Hi,

I'm trying to write a regular expression to mark up equations to HTML. Here is an example:
2^3 should become 2<sup>3</sup>
and a_b should become a<sub>b</sub>

However, I'm having trouble getting more complicated equations to properly work. For instance:
3^(1+(5*2)+3) should become 3<sup>1+(5*2)+3</sup>

The problem I'm having is that I need to match starting parentheses ( with ending parentheses ), while ignoring any other parentheses that may occur in the middle.

Is there a good regular expression way to do this?

PS - This is my first post here, so thanks in advance.


KevinR
Veteran


May 8, 2008, 9:33 AM

Post #2 of 4 (4478 views)
Re: [Wumbate] A way to check for "matching" characters? [In reply to] Can't Post

If all you ever needed was to substitute ^ with <sup> and then at the end put the closing tag </sup>that is probably doable.


Code
$r = '3^(1+(5*2)+3)'; 
$r =~ s#\^(.*)#<sup>$1</sup>#g;
print $r;


Anything else will be much more difficult.
-------------------------------------------------


Wumbate
New User

May 8, 2008, 10:53 AM

Post #3 of 4 (4471 views)
Re: [KevinR] A way to check for "matching" characters? [In reply to] Can't Post

That's not quite good enough... since something like this
2^4 + 3^3
would turn out as this
2<sup>4 + 3^3</sup>
when it should be this
2<sup>4</sup> + 3<sup>3</sup>

There's a similar problem for parsing HTML. What if you want to get everything inside the "start" span tag like this:
<span id="start">Outer span <span>inner span</span> more outer</span> <span>Second span</span>

If you want the whole first span tag, you can't just use
/\<span id=\"start\"\>.*?\<\\span\>/
because it will terminate at the first /span, but you also can't use
/\<span id=\"start\"\>.*\<\\span\>/
because it will terminate at the end of the last span.

Anyone know how this is taken care of?


KevinR
Veteran


May 8, 2008, 4:14 PM

Post #4 of 4 (4458 views)
Re: [Wumbate] A way to check for "matching" characters? [In reply to] Can't Post

Like I said: anything else will be much more difficult. Or at least with my limited experience it would be. Ask on www.perlmonks.com and see what suggests you get. Post back here if you get the solution.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives