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


In reply to Re: Regex pattern that fails when string contains a certain pattern by kcott
in thread Regex pattern that fails when string contains a certain pattern by hoppfrosch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.