Regex to detect patterns that do not start with // or <!--

M

Mike Grandmasion

Hi,

I am trying to write a pattern which will give me all matches in java
files for strings that contain the word "the " but do not start with
//, <!--.

This is my pattern so far,

grep -PRi 'out.print.*?[^/][^/].*?the' * | grep -Pi
'out.print.*?[^\<][^!][^-][^-].*?the'

It sort of works but I was wondering if anyone has suggestions on how
to improve it.

Thanks for any pointers,

Mike
 
M

Mike Grandmasion

It is a perl regular expression. That is what the P option
designates. If I wrote it in perl I would have the exact same
results.
 
M

Mike Grandmasion

m{^(?!//|<!--).*the }

Yeah I would have though I could negate a grouping like that too but
if I change my pattern from:

'out.print.*?[^/][^/].*?the'

to

'out.print.*?^(//).*?the'

I get no output.

-- This is my pattern so far,
--
-- grep -PRi 'out.print.*?[^/][^/].*?the' * | grep -Pi
-- 'out.print.*?[^\<][^!][^-][^-].*?the'

$ perl -w
grep -PRi 'out.print.*?[^/][^/].*?the' * | grep -Pi
String found where operator expected at - line 1, near "PRi 'out.print.*?[^/][^/].*?the'"
(Do you need to predeclare PRi?)
'out.print.*?[^\<][^!][^-][^-].*?the'
String found where operator expected at - line 2, near "Pi
'out.print.*?[^\<][^!][^-][^-].*?the'"
(Missing semicolon on previous line?)
syntax error at - line 1, near "PRi 'out.print.*?[^/][^/].*?the'"
Execution of - aborted due to compilation errors.
$


Well, that doesn't compile.

Right it is using a perl regular expression being executed through
grep on the unix commandline.
"sort of works"? Is that like "partially pregnant"?

No more like thinking you are pregnant with a baby and finding out you
are pregnant with two. My pattern is returing a little more than I
want.

Specifically I get hits on it like this:

out.print("<!--\r\n\t

because I assume that the pattern puts the <!-- into the minimal match
before the ^(<!--) portion.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top