views:

274

answers:

7

I would really like to use \w but it also matches underscores so I'm going with [A-Za-z] which feels unnecessarily verbose and America centric. Is there a better way to do this? Something like [\w^_] (I doubt I got that syntax right)?

+11  A: 

Perhaps you mean /[[:alpha:]]/? See perlre for the discussion of POSIX character classes.

zigdon
I still don't think this take the international characters into consideration, since `:alpha:` is still `a-zA-Z`
vol7ron
I was wrong; the perldocs should be updated. However it does include extra punctuations: `ʹʺʻˍˎˏːˑˬˮ̀́`
vol7ron
@vol7tron: Your comment is leaking...
Jon Purdy
@Jon Purdy: leaking?
vol7ron
@vol7ron: The extra punctuation spills beyond the comment into your username. (Firefox 3.6.10 on Windows XP)
Jon Purdy
@Jon Purdy: ahh, I suspect that's a side effect of having unicode characters in output - another reason not to use :alpha:
vol7ron
+5  A: 
[^\W0-9_]

# or

[[:alpha:]]

See perldoc perlre

runrig
+4  A: 

You could use /[a-z]/i or /[[:alpha:]]/ just as well. In fact, \w includes numbers so that won't even work.

Matt Kane
I take it the `i` makes it case-insensitive?
keithjgrant
That's correct.
Matt Kane
+1  A: 

you're looking for internationalization in your regex? then you'll need to do something like this guy did: http://stackoverflow.com/questions/1073412/javascript-validation-issue-with-international-characters

explicitly match on all of the moon language letters :)

Oren Mazor
I don't see any moon letters in that regex, but it might just be because I'm reading it in the daylight.
Matt Kane
When you see them, you're supposed to howl
vol7ron
+4  A: 

A few options:

1. /[a-z]/i               # case insensitive
2. /[A-Z]/i               # case insensitive
3. /[A-z]/                # explicit range listing (capital 'A' to lowercase 'z')
4. /[[:alpha:]]/          # POSIX alpha character class

I recommend using either the case-insensitive, or the true way /[a-zA-z]/, unless you have a certain language preference in mind.

Note:

  • Number 3 requires the capital 'A' first and then lowercase 'z' because of the order of the ASCII values; it does not work if you do the reverse: a-Z. Also: this method would fail the no-underscore criteria, since it includes [ \ ] ^ _ ` .
  • Number 4 will match on those additional language characters, but it also matches on:
    ʹʺʻˍˎˏːˑˬˮ̀́   (plus many others)
vol7ron
The `[A-z]` range includes a bunch of other characters.
NullUserException
You are correct there's a few brackets and extra characters, which would break the no-underscore criteria. I've modified the answer. @flies, yes but I was incorrect. Really the first two are the same method, just showing you can use different inputs.
vol7ron
+7  A: 

Matching international (i.e non-ASCII) characters is kind of tough, and could depend on a lot of things. Check out this example:

#!perl -w

use strict;
use utf8;

my $string = "ä";

print "matched :alpha:\n"  if $string =~ /[[:alpha:]]/;
print "matched ^\\W0-9_\n" if $string =~ /[^\W0-9_]/;
print "matched [a-zA-Z]\n" if $string =~ /[a-zA-Z]/;
print "matched [a-z]i\n"   if $string =~ /[a-z]i;
print "matched [A-z]\n"    if $string =~ /[A-z]/;

For me this results in

matched :alpha:

If you remove the use utf8 then none of the regular expressions match.

Looking at this very relevant question, it looks like you probably want to use utf8 and check out Unicode::Semantics.

Of course, if you're using straight ASCII characters than any of the aforementioned regular expressions will work.

CanSpice
You have to be careful with \w and \W in Perl. It's a bit of a moving target lately, so I wouldn't depend on its definition.
brian d foy
What your editor is displaying as 'ä' may not be a letter in Perl. 'ä' = "\xE4" is not defined in the ASCII character set, by default Perl will use ASCII character classes for string that are not encoded as UTF8. Adding `use utf8;` will treat your source code as UTF8 and make "ä" a Unicode string. In the Unicode character set 'ä' is a alpha character. Compare `$string = "ä";` vs `$string = decode("iso-8859-1", "ä");` without `use utf8;`. In the first case Perl will see binary data, in the second the binary data is being interpreted as Latin-1 and converted to UTF-8.
Ven'Tatsu
+6  A: 

Just use \p{L} which means "any Unicode letter" and works in Perl (/\p{L}/). You probably need to use utf8;.

Tim Pietzcker