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

I have the following regex
[^0-9a-zA-Z\-]
and I would like to match consecutive hyphens while still allowing single hyphen. I have tried but I do not know how to add something like [\-{1}] into my expression. To clarify my regex should match -- but not -.

2005-01-05 Janitored by Arunbear - added code tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: basic regex help please
by cfreak (Chaplain) on Jan 05, 2005 at 17:09 UTC

    You can't specify the number of something in a character class (the brackets, which I assume you meant to put but Perlmonks snarfed them. Try using >code< tags next time :) ). But you are on the right track. This should do what you want:

    /^(?:[0-9a-zA-Z]|\-{2,})/

    I'm assuming you want ^ to match the beginning of the line. You don't put that inside the brackets unless you want to match a literal ^. Also note the \-{2,}. The comma is nessicary because it specifies that you want 2 or more hyphens. Just a 2 and it would look for exactly two hyphens. (if that's what you want then remove the comma).

    I used ?: to keep the parenthisis from capturing the match, saves a little memory. You could take it out if you want to capture it for some reason.

Re: basic regex help please
by ikegami (Patriarch) on Jan 05, 2005 at 17:07 UTC
    /-{2,}|[^0-9a-zA-Z\-]/
Re: basic regex help please
by davido (Cardinal) on Jan 05, 2005 at 17:25 UTC

    Are you writing your own command line arguments parser when Getopt::Long already does that? Or is this some other situation unrelated to commandline options that requires you to locate fields starting with double hyphens? Just curious...


    Dave

      Thanks for your help everyone I know have the solution. Dave I'm just using this to sanitise form inputs from a web page.
Re: basic regex help please
by Moe (Novice) on Jan 05, 2005 at 17:13 UTC
    What I think you're looking for is -{2}. That specifically says: Look for two of the '-' character, in a row. Similarly, you could have (Bob){2} that would look for two 'Bob's in a row, as in BobBob.

    You can also set limits with curly braces. You want something that can take a two or four-digit year? \d{2,4}

    You want something that will match a string that you are guaranteed is at least 5 characters long, but you're not sure how long it may be? \w{5,}

    Hope this helps.
Re: basic regex help please
by William G. Davis (Friar) on Jan 05, 2005 at 17:05 UTC

    UPDATE:

    Missing code tags have foiled me again :)

      The OP forgot to escape his square brackets. He posted [^0-9a-zA-Z\-], not ^0-9a-zA-Z\-