[PLUG] Regex condition match problem

Colin Kuskie ckuskie at dalsemi.com
Tue Jun 29 09:05:03 UTC 2004


On Tue, Jun 29, 2004 at 08:17:13AM -0700, lbrigman at fcpa.fujitsu.com wrote:
> I have a regular expression problem that I have not be able to solve with 
> one
> regular expression.  I can solve it with two but would like to use just 
> one.
> 
> Problem description: A file that has multiple lines like the following 
> (max ten lines):
> "Detailed Test Description" testname "-d test-args more-test-args"
>
> The problem is that the last set of args may not be there at all.
> I need to match none, two or three fields and extract just the fields.
> The match for the first two without the third is as follows:
> ^"([\s\S]*)"(?:\s+)(\S+)
> 
> The match for all three is as follows
> ^"([\s\S]*)"(?:\s+)(\S+)(?:\s+)"([\s\S]*)"

If that's exactly the kind of input that you're looking for, then you
don't want to write your own regular expressions.  Based on that regular
expression syntax, it looks like you're using Perl, and it turns out that
perl comes with a module for parsing exactly the kind of input data that
you're working with.

It's called Text::ParseWords, and if you type:

perldoc Text::ParseWords

on the command line it will bring up the documentation for you
to read.

Good luck!
Colin




More information about the PLUG mailing list