0

I have a regular expression, but it does not work properly. The regular expression is:

m/^alias ([^\s]*) (.*) (<.*>)/

I tested my expression on a mutt file to convert it into a vcard. My string for the test is:

alias     john smith <john.smith@test.com>

But between alias and john I have a tab, and ([^\s]*) doesn't find the tab. Why not? Normally \s matches spaces or tabs, doesn't it?

ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
simon
  • 1,180
  • 3
  • 12
  • 33

2 Answers2

2

You have a literal space after alias in the pattern, so the first 6 characters of the string being matched must be "a", "l", "i", "a", "s" and space. But the sixth character of your string is a tab. I think you want the following:

my ($gname, $sname, $addr) = /^alias\s+(\S+)\s+(\S+)\s+<(.*)>/;

or

my ($name, $addr) = /^alias\s+([^<\s]+(?:\s+[^<\s])*)\s+<(.*)>/

(\S is equivalent to [^\s].)

ikegami
  • 367,544
  • 15
  • 269
  • 518
0

[^...] are negated character classes. \s is a wildcard for whitespace characters (including tabs: [ \t\r\n\f]). ([^\s]*) will capture every non-whitespace character.

As a side note, capitalizing a shorthand character class negates it. So, [^\s] is the same as \S.

Sam
  • 20,096
  • 2
  • 45
  • 71