Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Regex question : How to match \w and just the dash

by hmbscully (Scribe)
on Dec 17, 2004 at 21:00 UTC ( [id://415777]=perlquestion: print w/replies, xml ) Need Help??

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

I have rules for some database fields that only allow alphanumeric and the dash character. I am creating flatfile data to be imported and I am wanting to clean up the data submitted from a webpage form. I am thinking  s/^\W^-//g to strip out the unwanted characters. But it doesn't seem to work right. My regex skills aren't too fabulous. Pesky dash. TIA.

Replies are listed 'Best First'.
Re: Regex question : How to match \w and just the dash
by Eimi Metamorphoumai (Deacon) on Dec 17, 2004 at 21:07 UTC
    I'd probably use
    tr/-a-zA-Z0-9//cd;
    You could also do
    s/[^-a-z0-9]//ig;
    If you wanted a regexp solution. However
    s/[^-\w]//g;
    (which is what I think you think you're looking for) will not do what you want, because \w includes _.
Re: Regex question : How to match \w and just the dash
by Stevie-O (Friar) on Dec 17, 2004 at 21:08 UTC
    The regex /^\W^-/ will never match anything, because ^ only matches the beginning of a string, and you have a character (\W) before the second ^.

    I think that what you want is this:

    s/[^\w-]+//g
    That will strip out all characters that are neither \w nor a dash.
    --Stevie-O
    $"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc
Re: Regex question : How to match \w and just the dash
by ww (Archbishop) on Dec 17, 2004 at 21:14 UTC
    Think I have a glimmering... but just barely... of what your input might be. Input, please, and if ([A-Za-z\-])+ is not the desired out, a sample of that too? In fact, a tight sample of the script would make it MUCH easier to try to help.
      I'll give the offered solutions a go, they make sense. I did not realize that \W included the underscore. Nice to know. The input will be name and address fields on an order form.
        The input will be name and address fields on an order form.

        Then won't they contain spaces?



        ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
        =~y~b-v~a-z~s; print

        Just nit-picking, but keep in mind that \w and \W are not the same thing (note the case / capitalization). In fact, they are each other's complement: a character matched by \w will not be matched by \W, and vice versa.

        -- 
                dakkar - Mobilis in mobile
        

        Most of my code is tested...

        Perl is strongly typed, it just has very few types (Dan)

Re: Regex question : How to match \w and just the dash
by bradcathey (Prior) on Dec 18, 2004 at 14:06 UTC

    To clarify and to be totally A.R., I think you meant "hyphen." HTML provides for both kinds of dashes: 1. &ndash;, used for ranges of things (Monday–Friday); 2. &mdash;, used for breaks in thought ("Perlmonks—an online community—is a friendly place"), and, of course, the hyphen which is used for word breaks.

    All that to say: s/[a-zA-Z\-]+//g; would be my take, not seeing your input.


    —Brad
    "Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-24 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found