Regexp matches any uppercase letters except a specific string

I want to combine all strings with any uppercase letters in them, but ignoring the string A _

To add to the complexity, I want to ignore everything after another line, for example. open comment

Here are examples of what should and should correspond

Matches:

  • Foobar
  • foo bar foo
  • A_foobar
  • fooBar / * Comment * /

Not a match (C_ should not cause a match)

  • A_foobar
  • foo A_bar
  • Foobar
  • foo bar foo bar
  • foobar / * Comment * /

thanks:)

+5
source share
6 answers

This should (also?) Do this:

(?!A_)[A-Z](?!((?!/\*).)*\*/)

Brief explanation:

(?!A_)[A-Z]     # if no 'A_' can be seen, match any uppercase letter
(?!             # start negative look ahead
  ((?!/\*).)    #   if no '/*' can be seen, match any character (except line breaks)
  *             #   match zero or more of the previous match
  \*/           #   match '*/'
)               # end negative look ahead

So, in plain English:

, "A_", , "*/" "/*'.//p >

+3

Try:

(?<!A_)[a-zA-Z]+

(?!...) lookbehind.

, , :

^([#\.]|(?<!A_))[A-Za-z]{2,}

:

fooBar => fooBar
foo Bar foo => foo
A_fooBar (no match)
fooBar /* Comment */ => fooBar
A_foobar (no match)
foo A_bar => foo
foobar => foobar
foo bar foo bar => foo
foobar /* Comment */ => foobar
+1

:

/([B-Z]|A[^_]|A$)/

, .

:

#!perl
use warnings;
use strict;

my @matches = (
"fooBar",
"foo Bar foo",
"A_fooBar",
"fooBar /* Comment */");

my @nomatches = (
"A_foobar",
"foo A_bar",
"foobar",
"foo bar foo bar",
"foobar /* Comment */");

my $regex = qr/([B-Z]|A[^_]|A$)/;

for my $m (@matches) {
    $m =~ s:/\*.*$::;
    die "FAIL $m" unless $m =~ $regex;
}
for my $m (@nomatches) {
    $m =~ s:/\*.*$::;
    die "FAIL $m" unless $m !~ $regex;
}

: http://codepad.org/EJhWtqkP

+1

, . (, .)

.*((A(?!_)|([B-Z]))(?<!/\*.*)).*\r\n
0

? perl - :

if ($ string = ~/[A-Z]/& & $string! ~/A _/)

, , , , .

0

:

^(?:[^A-Z/]|A_|/(?!\*))*+[A-Z]

, , . PowerGrep, Java PHP. .NET , :

^(?>(?:[^A-Z/]|A_|/(?!\*))*)[A-Z]

, lookahead, A_ :

^(?:[^A-Z/]|A_|/(?!\*))*(?!A_)[A-Z]
0

All Articles