in reply to Regex pattern that fails when string contains a certain pattern
G'day hoppfrosch,
[Your post has undergone a number of changes. I didn't see the original. The one I'm replying to has (what appears to be) three updates: "Edit", "Edit2" and "Extended specification".]
"I'm looking for a regex-pattern, which should not match, when a certain pattern ... occurs within a string"
When checking for patterns that don't match, use '!~', as described in "perlop: Binding Operators", like this:
$string !~ /$pattern/
"... separator (underscore or minus) followed by number followed by any string ..."
You need to be more specific in prosaic descriptions like this. These are all numbers: 1, 10, 1.0, and so on. I'll assume you mean a single digit (following the separator), /\d/; if you need to be more specific than that, state your intent, e.g. /\d{n,m}/, /[0-9]/ or /\d/a. See "perlrecharclass: Digits" for further discussion. These are all strings: '', ' ', "\n", '1', '1.0', 'A', 'xyz', '%$#@', and so on. I'll assume you're not referring to zero-length strings or newlines, i.e. the first character after "number" matches /./. Again, state your intent if mean something more specific, e.g. /\S/, /\D/, and so on.
"What I hoped to work was: \A([A-Za-z1-9\.-_]*)(?:(?![-_]\d).)"
I really only wanted to comment on the bracketed character classes. '[-_]' and '[_-]' are both fine: the '-' is not special as the first (2nd, if 1st is a caret) or last character (elsewhere, it denotes a range). The '.' character is not special in a bracketed character class; '[\.-_]' and '[.-_]' both mean the same thing: the complete range of the 50 characters with decimal ASCII codes between 46 and 95, inclusive. See "perlrecharclass: Bracketed Character Classes" for a lot more details on these points.
"String aa1_2 should not be matched"
Either that's wrong or your description is. 'aa1_2' neither starts with a number nor has a separator followed by a number followed by a string.
Here's the regex that I think you don't want to match:
/(?:^\d|[_-]\d.)/
Here's a quick test:
$ perl -E 'my @x = qw{aa1.2 aa1_bb2 aa1_2 aa1_2bb 1aa}; $_ !~ /(?:^\d| +[_-]\d.)/ && say for @x' aa1.2 aa1_bb2 aa1_2
— Ken
|
|---|