John W. Krahn wrote at Tue, 20 Aug 2002 22:43:55 +0200:
> Shawn wrote:
>> I have two goals I would like help with:
>> 1. Optimize/simplify the regex
>> 2. Recognize where the regex would produce false positives on some
>> ifDescrs which don't actually have a real hardware path
More important is always to improve readability first !
>> ...
>> Now, I have the following regex to split these up into four pieces.
>> /^([^0-9]+)([0-9]+ |)([0-9]+\/[^\s]+)(.*)$/
>> $1 : Is a required match of some non-numeric chars. This would
>> match "Ethernet", or the "T" in T3 or T1
>> $2 : An optional match which would catch the "3 " in ifDescrs
>> like "T3 3/0/1"
>> $3 : string starting with one or more numerics, then a "/", then
>> all non-whitespace
>> $4 : the rest
>> ...
> You can use \d instead of [0-9], \D instead of [^0-9] and \S instead of
> [^\s].
>
> /^(\D+)(\d+ |)(\d+\/\S+)(.*)$/
^^^^^^^
That's still an ugly and slow way to have an optional match
better write it as
m!^(\D+)(\d+ )?(\d+/\S+)(.*)$!
What I still don't understand is why it's necessary to capture the rest in $4.
In the given examples
"Ethernet1/0", "T3 3/0/2", "POS2/0/1"
there is no rest.
Could it also be that the first part is simply a name
that could have some digits at the end and
the second part is a path.
If the name ends on a digit,
then the name and the path are seperated with a blank.
I ask, as this job could be done quite easier:
m!^(\w+?) ?([\d/]+)$/ [untested]
allthough I don't believe that this solution is quicker.
Cheerio,
Janek
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]