Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

regular expression question

0 views
Skip to first unread message

Orson

unread,
May 15, 2008, 2:48:26 PM5/15/08
to
Sorry, this is probably silly question, but I cannot find right
answer.

I am trying to use grep -P (for perl regular expressions). I want to
match lines that do not contain the chars "sync".

I thought I could do:

grep -P "[^(sync)]" mytextfile

I understand [] matches any char in the brackets and (sync) groups
those chars together, so I would negate to mean should not match those
chars.

This is return all lines in the file. I am doing something wrong, just
do not know what. Can someone explain, please?

Jim Gibson

unread,
May 15, 2008, 3:12:49 PM5/15/08
to
In article
<1abd8794-b762-400e...@d77g2000hsb.googlegroups.com>,
Orson <moles...@yahoo.com> wrote:

> Sorry, this is probably silly question, but I cannot find right
> answer.
>
> I am trying to use grep -P (for perl regular expressions). I want to
> match lines that do not contain the chars "sync".

Does your grep have the '-v' switch ('--invert-match'):

grep -v sync

>
> I thought I could do:
>
> grep -P "[^(sync)]" mytextfile
>
> I understand [] matches any char in the brackets and (sync) groups
> those chars together, so I would negate to mean should not match those
> chars.

Parentheses group, but not inside brackets. "[^(sync)]" will match any
character other than the 6 listed. In Perl, you would negate the test:

$string !~ /sync/;

but that won't work as an option for grep. My grep doesn't have a '-P'
switch, so I can't tell how to use it. However, the -v switch seems to
do what you want.

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
http://www.usenet.com

Gunnar Hjalmarsson

unread,
May 15, 2008, 3:22:24 PM5/15/08
to
Orson wrote:
> I am trying to use grep -P (for perl regular expressions). I want to
> match lines that do not contain the chars "sync".
>
> I thought I could do:
>
> grep -P "[^(sync)]" mytextfile
>
> I understand [] matches any char in the brackets and (sync) groups
> those chars together, so I would negate to mean should not match those
> chars.

The parens don't group within brackets, so you match every line with any
character other than
( s y n c )

Try using the -v option instead.

grep -Pv "sync" mytextfile

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Orson

unread,
May 15, 2008, 4:33:10 PM5/15/08
to
On May 15, 3:12 pm, Jim Gibson <jimsgib...@gmail.com> wrote:
> In article
> <1abd8794-b762-400e-9079-4876c603e...@d77g2000hsb.googlegroups.com>,

Yes, this is interesting and it works. I was trying to get the same
you get in perl when you do $string !~ /sync/ as you mentioned. I have
a solution to problem and I thank you for that. Out of curiosity how
would you negate a word with regex with grep if you didn't have the -v
option or anything like it ? I am not sure I know because my way as
just matching characters, but you want to say lines that do NOT have
the word "sync". Perhaps there is no way using regex.

Abigail

unread,
May 15, 2008, 4:55:46 PM5/15/08
to
_
Orson (moles...@yahoo.com) wrote on VCCCLXXI September MCMXCIII in
<URL:news:1abd8794-b762-400e...@d77g2000hsb.googlegroups.com>:
,, Sorry, this is probably silly question, but I cannot find right
,, answer.
,,
,, I am trying to use grep -P (for perl regular expressions). I want to
,, match lines that do not contain the chars "sync".
,,
,, I thought I could do:
,,
,, grep -P "[^(sync)]" mytextfile
,,
,, I understand [] matches any char in the brackets

Yes, it does.

,, and (sync) groups
,, those chars together

Why do you think you need to group things? Apparently, you think there
is a difference between:

[ab]

and

[(ab)]

and I am very interested to know what kind of difference you thing that
can be.

With a few extreme exceptions, [ ... ] matches a *SINGLE* character.
Regardless of the number of characters it lists inside the brackets.

I'm curious, if you think that /[sync]/ matches any string that contains
the substring "sync" (an no string that doesn't contain it), what do you
think that /sync/ matches?


,, so I would negate to mean should not match those
,, chars.

Right, but not as you think it does. /[^(sync)]/ matches any string that
doesn't consists of just '(', 's', 'y', 'n', 'c' and ')' characters.

,, This is return all lines in the file. I am doing something wrong, just
,, do not know what. Can someone explain, please?


First, if you are using grep, use the -v option to negate.

If you're looking for a perl regex that matches any string that doesn't
contain 'sync', use:

/^(?:(?!sync).)*$/s

Or, after some loop unrolling:

/^[^s]*(?:s(?!ync)[^s]*)*$/


Abigail
--
package Z;use overload'""'=>sub{$b++?Hacker:Another};
sub TIESCALAR{bless\my$y=>Z}sub FETCH{$a++?Perl:Just}
$,=$";my$x=tie+my$y=>Z;print$y,$x,$y,$x,"\n";#Abigail

s...@netherlands.co

unread,
May 15, 2008, 5:21:25 PM5/15/08
to
On 15 May 2008 20:55:46 GMT, Abigail <abi...@abigail.be> wrote:

?! does not work
> /^(?:(?!sync).)*$/s
^ ^ ^
and whats are these? "." ??????

Jürgen Exner

unread,
May 15, 2008, 10:48:42 PM5/15/08
to
Orson <moles...@yahoo.com> wrote:
>I am trying to use grep -P (for perl regular expressions). I want to
>match lines that do not contain the chars "sync".

Do you mean contains none of the characters 'c', 'n', 's', and 'y'?
Or do you mean does not contain all of those 4 characters?
Or do you mean does not contain the substring 'sync'?
There are very different requirements and your description can be
interpreted in all three way and it is not clear to me what you expect.

jue

s...@netherlands.co

unread,
May 17, 2008, 3:54:38 PM5/17/08
to

I guess its got me plonkd.
We should probably get past "?! does not work" first before determining


> /^(?:(?!sync).)*$/s
> ^ ^ ^
>and whats are these? "." ??????

which are illustrative embelishment at best.

0 new messages