• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 35
  • Last Modified:

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!
0
willsherwood
Asked:
willsherwood
  • 8
  • 6
  • 2
2 Solutions
 
Marco GasiFreelancerCommented:
Use this:

(?<!=db_)\w+
0
 
it_saigeDeveloperCommented:
/(db_ABC)+|ABC+/g will provide all matches of db_ABC in group 1 and all matches of ABC in group 0.

-saige-
0
 
willsherwoodAuthor Commented:
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_
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Marco GasiFreelancerCommented:
Then this:

(?<!=db_)\ABC
0
 
Marco GasiFreelancerCommented:
Sorry

(?<!db_)ABC
0
 
willsherwoodAuthor Commented:
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
0
 
willsherwoodAuthor Commented:
ooops  nevermind, that last one matches the entire line  :(
0
 
Marco GasiFreelancerCommented:
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
0
 
willsherwoodAuthor Commented:
(?<!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
0
 
willsherwoodAuthor Commented:
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:)
0
 
willsherwoodAuthor Commented:
(?<!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!
0
 
it_saigeDeveloperCommented:
https://www.regex101.com/ is an excellent online tester

-saige-
0
 
Marco GasiFreelancerCommented:
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 :)
0
 
willsherwoodAuthor Commented:
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!
0
 
Marco GasiFreelancerCommented:
Great! I was very surprised to hear that didn't work :-)
0
 
willsherwoodAuthor Commented:
Thanks all!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 8
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now