moltar512 has asked for the wisdom of the Perl Monks concerning the following question:
It works great.. but that regular expression.. Jesus Christ.. Is there a way to dumb that down for me so that i can apply it to other situations, or would that take a complete O'Reilly book to figure out?$A = -4; $w = 500; then $re = '/^\s*(\Q$A\E\s*\*?\s*\Qcos\E\s+\Q$w\E\s*\Qt\E\s+\QA\E||\Q$A\E\s*\*?\s +*\Qcos\E\s*\(\s*\Q$w\E\s*\Qt\E\s*\)\s+\QA\E)\s*$/'; will accept answers in the following form: -4cos 500t A -4 cos 500t A -4*cos 500t A -4*cos(500t) A -- and any number of spaces before and after *, t, ( +and )
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: is there an easy way to dumb down this regular expression for me?
by japhy (Canon) on Oct 19, 2005 at 20:26 UTC | |
As for dumbing it down, I'd rewrite it like so: Then you can use it in the following manner: $string =~ /^\s*($rx)\s*$/. | [reply] [d/l] [select] |
by diotalevi (Canon) on Oct 19, 2005 at 20:42 UTC | |
A nit - you'll notice that $re is assigned '/.../', not a qr expression. I prefer your assignment of the qr// but its worth noting. | [reply] [d/l] |
by moltar512 (Sexton) on Oct 19, 2005 at 20:48 UTC | |
| [reply] |
by davidrw (Prior) on Oct 19, 2005 at 21:35 UTC | |
x Extend your pattern's legibility by permitting whitespace and comments.It was used so that everything could be spaced out for clarity -- that's also why al the \s* are in there because when using /x you have to explicitly say when you actually want to match whitespace. | [reply] [d/l] [select] |
|
Re: is there an easy way to dumb down this regular expression for me?
by diotalevi (Canon) on Oct 19, 2005 at 20:41 UTC | |
You had a bug. The alternation || is only written with *one* pipe character. The zero length pattern between each of the |s is also available so you could match anything that /^\s*$/ would match. The \Q...\E means quotemeta(). I've removed the \Q$A\E by doing quotemeta outside teh regexp. Since quotemeta has no effect on stuff like t and A, I removed it from those as well. I factored out the common parts of the expression so they aren't repeated. I also used the /x flag so I could add in as much extra whitespace as a I want - makes it easier to read. You'll notice how now everything except one branch of the alternation is followed by optional whitespace. That's to prevent "tA" from matching.
| [reply] [d/l] |
|
Re: is there an easy way to dumb down this regular expression for me?
by GrandFather (Saint) on Oct 20, 2005 at 00:28 UTC | |
Fixing the || bug, refactoring the expression and commenting gives this:
Perl is Huffman encoded by design. | [reply] [d/l] [select] |
|
Re: is there an easy way to dumb down this regular expression for me?
by ioannis (Abbot) on Oct 20, 2005 at 03:15 UTC | |
Thanks to GrandFather who styled and commented the whole mess. The pattern now takes a huge space on my screen and the Perl code is mixed with typical ugly regexes. So, how do I reuse the pattern in another module; simple you say, declare it with our so the pattern is accessable from everywhere. Could we do better and avoid the our declaration? Yes, you say, declare a function that returns the regex, and all we have to do later is check for match using $_ =~ cos_pattern() . Lets declare the function: sub cos_pattern { $re } You see the problem? We have inadvertently created a closure. And how do we solve this problem: back declaring with our again:
| [reply] [d/l] [select] |
|
Re: is there an easy way to dumb down this regular expression for me?
by Anonymous Monk on Oct 21, 2005 at 14:56 UTC | |
| [reply] [d/l] |