Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

PHP and Regular Expressions

Name: Anonymous 2007-06-15 14:38 ID:kQPwzXix

I'm not quite the EXPERT PROGRAMMER when it comes to regular expressions. I has a string:

$eq = "If the bobsled has to go 30 meters in 5 seconds starting from rest, then I think we could find the acceleration using: %eDeltax = (1/2) a t^2 + v_(0)te% Solving for acceleration would yield: %ea = 2(Deltax)/t^2e% since %ev_(0)e% is zero %ea = 2(30 m)/(5 s)^2 = 2.4e%%em/s^2e%";

I want to extract everything between the %e and e% using preg_match_all() but I don't do good with regular expressions - what would you do? I can perform this task using strlen(), strpos() and strtr() but this seems like terrible overkill

Name: Anonymous 2007-06-15 14:51 ID:Heaven

%e(.*?)e%

Name: Anonymous 2007-06-15 14:56 ID:kQPwzXix

>>2
close - my first guess was /%e*e%/ but that returned empty - your %e(.*?)e% kind of worked, but it need to be

'/%e(.*?)e%/' - now can someone tell me why '(.*?)' works but not '*'?

Name: Anonymous 2007-06-15 15:07 ID:Heaven

>>3
'*' just means to match the last character/character class/subexpression/whatever 0 or more times.
so /%e*e%/ would match '%e%', '%ee%', '%eee%', '%eeee%', etc.
'.' means any character, and parentheses delimit subexpressions.
the '?' makes '.*' match as many characters as possible instead of as few as possible (which is the default).

tldr: you're an idiot and should go read http://perldoc.perl.org/perlretut.html until you understand how this works:
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

Name: Anonymous 2007-06-15 17:33 ID:cFDMIznG

It should be "(?<=%e)(.*?)(?=%e)".

| the '?' makes '.*' match as many characters as possible instead of as few as possible (which is the default).

Other way around.

Name: 4 2007-06-15 18:29 ID:Heaven

>>5
you weren't supposed to tell him that.

Name: Anonymous 2007-06-15 19:04 ID:147hGODh

>>6
Is that in any way relevant to this topic, or is it just your way of saying "I've read SICP?"

Name: Anonymous 2007-06-15 19:05 ID:qD1ZMdAU

>>6
>>7
Why are you putting the post link inside the spoiler?

Name: Anonymous 2007-06-15 20:37 ID:147hGODh

>>8
I am amused by doing this

Name: Anonymous 2007-06-15 20:51 ID:cBgerGqN


>>9
Test

Name: Anonymous 2007-06-16 13:09 ID:GvgqC/qw

lol spoilers

Name: Anonymous 2007-06-16 13:21 ID:Heaven

>>1
Is dat sum EXPERT wheel reinvention?

Use \LaTeX, latex2html, etc.

Name: Anonymous 2007-06-16 13:30 ID:X8LnhrgA

>>12
That is probably what he's trying to do. Extracting what's in between the %e e% tags, pass it to LaTeX and then replace the text with an image of the LaTeX output like Wikimedia does.

Name: Anonymous 2007-06-16 13:40 ID:NoNuBuqP

>>13
The language he is using is not TeX, though. So, instead of reusing already existing code, he'll probably end up writing his own parser and renderer.

Name: Anonymous 2007-06-17 2:39 ID:bYoZZ5SS

>>12
>>13
>>14
Sort of - I'm passing it through some god-awful latex/algebra script thats been in place for years before I started working on this stuff. Someone else reinvented the code before I got there, now I just have to maintain it and I don't get paid to bring it up to standards with, well, standards, I get paid to put duct tape on it and pray that it doesn't catch fire : /

Name: Anonymous 2011-11-10 11:22

old thread is old

Don't change these.
Name: Email:
Entire Thread Thread List