Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex mixed case excluding specific case

I need a regex able to match:

  • a) All combinations of lower-/upper-cases of a certain word
  • b) Except a couple of certain case-combinations.

I must search the bash thru thousands of source-code files, occurrences of miss-spelled variables.

Specifically, the word I'm searching for is FrontEnd which in our coding-style guide can be written exactly in 2 ways depending on the context:

FrontEnd (F and E upper)
frontend (all lower)

So I need to "catch" any occurences that do not follow our coding standards as:

frontEnd
FRONTEND
fRonTenD

I have been reading many tutorials of regex for this specific example and I cannot find a way to say "match this pattern BUT do not match if it is exactly this one or this other one".

I guess it would be similar to trying to match "any number between 000000 to 999999, except exactly the number 555555 or the number 123456", I suppose the logic is similar (of course I don't knot to do this either :) )

Thnx


Additional comment:

I cannot use grep piped to grep -v because I could miss lines; for example if I do:

grep -i frontend | grep -v FrontEnd | grep -v frontend

would miss a line like this:

if( frontEnd.name == 'hello' || FrontEnd.value == 3 )

because the second occurence would hide the whole line. Therefore I'm searching for a regex to use with egrep capable to do the exact match I need.

like image 950
Xavi Montero Avatar asked Dec 14 '25 14:12

Xavi Montero


1 Answers

You won't be able to do this easily with egrep because it doesn't support lookaheads. It's probably easiest to do this with perl.

perl -ne 'print if /(?!frontend|FrontEnd)(?i)frontend/;'

To use just pipe the text through stdin

How this works:

perl -ne 'print if /(?!frontend|FrontEnd)(?i)frontend/;'
^     ^^  ^     ^  ^ ^ ^                 ^   ^ The pattern that matches both the correct and incorrect versions.
|     ||  |     |  | | |                 | This switch turns on case insensitive matching for the rest of the regular expression (use (?-i) to turn it off) (perl specific)
|     ||  |     |  | | | The pattern that match the correct versions.
|     ||  |     |  | | Negative forward look ahead, ensures that the good stuff won't be matched
|     ||  |     |  | Begin regular expression match, returns true if match
|     ||  |     | Begin if statement, this expression uses perl's reverse if semantics (expression1 if expression2;)
|     ||  | Print content of $_, which is piped in by -n flag
|     || Evaluate perl code from command line
|     | Wrap code in while (<>) { } takes each line from stdin and puts it in $_
| Perl command, love it or hate it.
like image 157
OmnipotentEntity Avatar answered Dec 19 '25 06:12

OmnipotentEntity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!