Results 1 to 2 of 2

Thread: Which Perl regex character matches punctuation characters etc

  1. #1
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Which Perl regex character matches punctuation characters etc

    Hi all,

    I have the following:
    PHP Code:
    if(!preg_match("/^[a-zA-Z0-9\s]+$/",$value)) 
    Which accepts Hello4 as valid input

    But I need it to allow any punctuation too i.e. !?/\<>.,'"@()[]

    Is there a character/modifier for this?

    Thanks

    Chris.

  2. #2
    Join Date
    Aug 2007
    Location
    Ohio
    Posts
    79
    Thanks
    0
    Thanked 15 Times in 15 Posts

    Default

    Hey Chris,

    As far as I know, there are no shorthand sequences for punctuation, but you can definitely still achieve that same effect through creating a character class.

    For example, if you wanted the code you posted to include those punctuation characters you listed too, you'd change the pattern to the following:

    Code:
    /^[a-zA-Z0-9\s!?/\\<>.,'"@()[\]\-]+$/
    Note that only the -, \, ] and ^ characters need to be escaped within a character class.

    I wonder what you're trying to prohibit or search for with this pattern? It might be easier to create a negative class and list any characters you don't want rather than all the characters you do.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •