Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regular Expressions
by imp (Priest) on Nov 27, 2006 at 14:08 UTC | |
The output is as follows: Another useful tool is to use the 'x' modifier to allow whitespace in the regex. I consider regex to be an extremely dense programming language, and without the whitespace to organize your thoughts it is very easy to get lost in the noise. Here is your regex, using the 'x' modifier: When writing a large regex it is a tradeoff between accuracy and readability. It is sometimes tempting to keep it simple so the regex is maintainable. 'x' is useful for addressing this problem, as you can put comments in the regex. Here is a revised regex for you: And if you would like to make it more readable you can separate some of the tokens into other variables, like this: I noticed that your example input allowed '=>' instead of '>=', maybe in your locale that is allowed? Here is a functional test script for you. It matches the items documented in the regex, but does not match '=>' or '=<' (Is that allowed in your locale?)
| [reply] [d/l] [select] |
|
Re: Regular Expressions
by johngg (Canon) on Nov 27, 2006 at 14:17 UTC | |
which gives this output
Given a more complex set of operators you would probably be better off taking grinder's approach of setting up a regular expression that matches and captures any operator. As complexity increases a parser solution becomes more appropriate. I hope this is of use. Cheers, JohnGG | [reply] [d/l] [select] |
|
Re: Regular Expressions
by grinder (Bishop) on Nov 27, 2006 at 14:03 UTC | |
=< doesn't look like any operator I've ever met, but still, assuming you want to match =, !=, <, >, <= and >=, then note that ther operands involving less than and greater than are different, in that they may be followed by an = (equals), accounting for two more operators for free. That gives us [<>]=?That leaves = and !=. This is just equals, maybe preceded by an excla. This gives !?=Now all the operators have been accounted for. Putting them together in a capturing group with an alternation gives: ([<>]=?|!?=)Dividing the atoms you want to match into different groups is usually the best way of coming up with an expression that matches all of them. Also, you want to consider patterns that share a similar beginning, since this way you'll end up with a regular expression that doesn't have to backtrack. If you really meant to match =<, then with the above approach you should be able to come up with something that works. Look at all the operators that start with an =, and then the remaining operators that don't. • another intruder with the mooring in the heart of the Perl | [reply] [d/l] |
by ambrus (Abbot) on Nov 27, 2006 at 21:48 UTC | |
| [reply] [d/l] [select] |
|
Re: Regular Expressions
by Locutus (Beadle) on Nov 27, 2006 at 14:39 UTC | |
Anyway, your current regular expression won't recognize the operators != or => at all. Unless your program is supposed to recognize and handle the input of undefined operators split /(\W+)/ (as suggested above) might be a sufficient alternative. If you want to throw an error message on something like a=[b you can use /^(.*?)(!=|=>|=<|[<=>])(.*)/ and react appropriately if there's no match. | [reply] [d/l] [select] |
|
Re: Regular Expressions
by Anonymous Monk on Nov 27, 2006 at 12:46 UTC | |
| [reply] [d/l] [select] |
|
Re: Regular Expressions
by Moron (Curate) on Nov 27, 2006 at 17:22 UTC | |
-M Free your mind | [reply] |