Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Regex confusion

by neilwatson (Priest)
on Aug 07, 2002 at 16:30 UTC ( #188372=perlquestion: print w/replies, xml ) Need Help??

neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

Consider this snipped:

$x = 'o\&a.sysadm'; if ($x =~ m/^(\w+[\\\&]?-?\w*)\.([\w+\\?[\\\&]?-?\w*\.?]+)/i){ $client = $1; $project = $2; }

Why does $client = nothing and $project = o\&a.sysadm?

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Regex confusion
by suaveant (Parson) on Aug 07, 2002 at 16:36 UTC
    [\\\&]? matches zero or one characters & or \... \& is two characters long...

    maybe you want [\\\&]{0,2}

    Update Thinking about it... if you are trying to match \& only.. and not &\ \\ or && then you probably really want... (?:\\&)? which will match the string \& 0 or 1 times, and not capture it (?:)

                    - Ant
                    - Some of my best work - (1 2 3)

Re: Regex confusion
by arturo (Vicar) on Aug 07, 2002 at 16:44 UTC

    I try that and get complete failure (the regex doesn't match at all).

    That second grouping has some odd stuff going on in there; you start a character class, but have another one embedded inside it and it looks like you're also trying to use quantifiers in there, then you have an unescaped - which attempts to specify that you're seeking to match a range between what goes before and what goes after.

    So let's step back one square: what kinds of things are you trying to match?

    Presented with *this* example, I'd go much simpler and do:

    if ( $x=~ /([^.]+)\.(\S+)/ ) { ... }

    Unless those patterns are too forgiving ...

    I mistrust all systematizers and avoid them. The will to a system shows a lack of integrity -- F. Nietzsche

      So let's step back one square: what kinds of things are you trying to match?

      Trying to match: client.project.subproject where:

      • Client and project are manditory. Subproject is optional but, there could be more than one.
      • Client, project and subproject could contain, letters, numbers, _, -, or \&
      Does that help?

      Update:Actually your regex /([^.]+)\.(\S+)/ works well. Thank you.

      Neil Watson
      watson-wilson.ca

        OK, to match those possible characters (and only those) try something like
        m/((?:\w|\\\&|\-)*)\.((?:\w|\\\&|\-)*)/
        the \w takes care of letters, numbers and _. I always escape the - out of habit but is unnecessary. The /i is redundant since the \w is case insensitive.
        Personally I would do a nongreedy match. It will match other characters but it probably won't matter. Something like:
        m/(.*?)\.(.*)/
        update -- of course a simple split would do the trick.
        my ($client, $project, $subproject) = split /\./, $x;

        --

        flounder

        Arturo is right -- I don't see how anything is getting assigned to $project, since the match does not succeed at all for the initial example.

        Trying to match: client.project.subproject where:

        • Client and project are manditory. Subproject is optional but, there could be more than one.
        • Client, project and subproject could contain, letters, numbers, _, -, or \&

        Update:Actually your regex ... works well. Thank you.

        Arturo's regex will include both your project and all sub-projects in $2. If that's okay, fine. If not, do it like this:

        /([^.]+)\.([^.]+)/
Re: Regex confusion
by thelenm (Vicar) on Aug 07, 2002 at 16:40 UTC
    I think you probably want parentheses instead of square brackets around \\\&, like this:
    $x = 'o\&a.sysadm'; if ($x =~ m/^(\w+(?:\\\&)?-?\w*)\.((?:\w+\\?(?:\\\&)?-?\w*\.?)+)/i){ $client = $1; $project = $2; }
    Update: changed a [...] I had missed into (?:...).

    -- Mike

    --
    just,my${.02}

Re: Regex confusion
by I0 (Priest) on Aug 08, 2002 at 00:33 UTC
    Because the if condition fails and you had $client = nothing and $project = o\&a.sysadm from earlier
    What do you want in $client and $project?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://188372]
Approved by arturo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2023-09-27 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?