A negative PCRE result gives an unexpected result

I want the Perl regex to match std::foo, but not to match std::foo::bar. This is what I have so far:

/((?<!\w)([A-Za-z0-9_]+)::([A-Za-z0-9_]+))(?!:)/

This matches std::foo::barup to std::fo, but I want the whole match to fail for this input, rather than giving a partial match.

What regular expression do I need?

+4
source share
4 answers

foo \w++. , "" , - - . , - :, baz::std::foo

. \w , /x .

use strict;
use warnings 'all';
use feature 'say';

my $s = 'match std::foo but not match std::foo::bar.';

say $1 while $s =~ / (?<![\w:]) ( \w+::\w++) (?!:) /gx;

std::foo
+4

\b , , exisst, : . .

((?<![:\w])([A-Za-z0-9_]+)::([A-Za-z0-9_]+))\b(?!:)

, .

(?<!\S)([A-Za-z0-9_]+)::([A-Za-z0-9_]+)\b(?!:)

DEMO

+1

\b :

  • , foo: ((?<!\w)([A-Za-z0-9_]+)::([A-Za-z0-9_]++))(?!:)
  • lookahead, foo: ((?<!\w)([A-Za-z0-9_]+)::([A-Za-z0-9_]+))(?![:\w])
+1

std::foo::bar

((\b(.+)\b)+?[::]{2}.+)

  • std::foo
  • std::foo::bar
-1
source

Source: https://habr.com/ru/post/1670733/


All Articles