http://qs1969.pair.com?node_id=188379


in reply to Regex confusion

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

Replies are listed 'Best First'.
Re: Re: Regex confusion
by neilwatson (Priest) on Aug 07, 2002 at 16:54 UTC
    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

        update -- of course a simple split would do the trick.

        Neil bangs his head vigourously against his desk. Of course! Why the hell didn't I think of that before?

        Thank you all for your input.

        Neil Watson
        watson-wilson.ca

      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:

      /([^.]+)\.([^.]+)/