Avatar of willsherwood
willsherwood
 asked on

REGEX simple ! question

I want to match all occurrences of ABC that are not preceded by  db_

xyzABC    will match
xyzdb_ABC   won't match

i tried
*(!db_)ABC  
but it still matches.

please help find my mistake

thanks!
Regular Expressions

Avatar of undefined
Last Comment
willsherwood

8/22/2022 - Mon
Marco Gasi

Use this:

(?<!=db_)\w+
it_saige

/(db_ABC)+|ABC+/g will provide all matches of db_ABC in group 1 and all matches of ABC in group 0.

-saige-
willsherwood

ASKER
sorry for the confusion.
ABC is exact literal text (not meta placeholder text for my example)
i don't want to match ABC   when  the ABC identifier is directly preceded by  db_
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Marco Gasi

Then this:

(?<!=db_)\ABC
ASKER CERTIFIED SOLUTION
Marco Gasi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
willsherwood

ASKER
hmmmm  sorry, plain ABC doesn't match now.
i experimented some more and tried
*!(db_)ABC

and that seems to work (i hope) - does that look reasonable?
(i had the not operator in the wrong place
willsherwood

ASKER
ooops  nevermind, that last one matches the entire line  :(
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Marco Gasi

Try this
(?<!db_)ABC(?x:)

The last expression means 'Ignore Pattern/Whitespace'.
With this I get 3 matches using following text:

xyzABC    will match
xyzdb_ABC   won't match
ABC
DSDSABC
willsherwood

ASKER
(?<!db_)ABC    unfortunately does not match anything  (even just ABC)

to recap:
i'd like the following examples to match:
(nothing)ABCetc1
123ABCetc2
_ABCetc3

but the following
not to match:
(nothing)db_ABCetc1
anythingdb_ABCetc2

thanks
willsherwood

ASKER
I suspect my simplification is causing the issue for the test example, my apologies.

here's the real life example being searched for:
(?<!db_)SingleFieldQuery(x:)
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
willsherwood

ASKER
(?<!db_)SingleFieldQuery(?x:)    matches nothing, sorry.

plain SingleFieldQuery matches, for example:

$CustAcctID = SingleFieldQuery("SELECT

and

// db_SingleFieldQuery       (don't want this matched)

is there an online Regex tester (i found several that are installable programs, but none that
one can enter a searchstring and a sample text to search/replace)

(i'm wondering if it's a bug in the particular implementation of regex in the tool i'm using? )

thanks!
SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Marco Gasi

In the suggested tester the regex works without the parameter to Ignore PatternWhiteSpace required by my tester. So it seems work the regex suggested in my post ID: 40692307. I'm a bit confused about the reason one tester require some additiona parameter and other don't... Anyway, good link, @saige :)
willsherwood

ASKER
aha!  excellent,
it's evidently a bug in the regex engine for the tool i'm using!

THANK YOU

(?<!db_)SingleFieldQuery    works to differentiate the two test cases!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Marco Gasi

Great! I was very surprised to hear that didn't work :-)
willsherwood

ASKER
Thanks all!