9 Replies - 805 Views - Last Post: 28 July 2009 - 02:51 PM Rate Topic: -----

#1 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Regex Trouble

Posted 28 July 2009 - 09:22 AM

My goal is to print all lines in a file not containing the characters a through f.

Is this:
^[^a-f]+$

correct?

I'm using grep, and
grep ^[^a-f]+$ dict | head

is matching nothing.

halp me DIC!
(dict is attached.)

(edited to reflect info below)

Attached File(s)

  • Attached File  dict.txt (626.64K)
    Number of downloads: 47

This post has been edited by lanec42: 28 July 2009 - 02:27 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Regex Trouble

#2 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Regex Trouble

Posted 28 July 2009 - 09:57 AM

Has anyone really been far even as decided to use even go want to do look more like?
Was This Post Helpful? 0
  • +
  • -

#3 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 911
  • View blog
  • Posts: 3,180
  • Joined: 12-May 09

Re: Regex Trouble

Posted 28 July 2009 - 09:59 AM

View Postlanec42, on 28 Jul, 2009 - 12:57 PM, said:

Has anyone really been far even as decided to use even go want to do look more like?


Wait, what?
Was This Post Helpful? 0
  • +
  • -

#4 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Regex Trouble

Posted 28 July 2009 - 10:01 AM

I was just bampin mah threadz ;)
Was This Post Helpful? 0
  • +
  • -

#5 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Regex Trouble

Posted 28 July 2009 - 02:02 PM

Your regex will matches anything that has a letter not within the range of a - f. That's why 'aardvark' matches because it has an 'r', and that's why 'a ' matches because you have a blank space in there.

Try:

^[^a-f]+$

It should match all lines that do not begin with, end with, nor contain any letters in the range of a-f, and contains at least 1 letter. You'll need to make it case-insensitive if you don't want to match A-F either, so you can either put A-F in the brackets as well, or add the case-insensitive option. I think it's like this:

grep -i ^[^a-f]+$ filename
Was This Post Helpful? 1
  • +
  • -

#6 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Regex Trouble

Posted 28 July 2009 - 02:17 PM

Posted Image

That makes sense. Thanks!

EDIT: Wait, it's still not working for me. No matches. Hmm.

This post has been edited by lanec42: 28 July 2009 - 02:30 PM

Was This Post Helpful? 0
  • +
  • -

#7 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Regex Trouble

Posted 28 July 2009 - 02:33 PM

Why not just [^a-f]+ ?
Was This Post Helpful? 0
  • +
  • -

#8 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Regex Trouble

Posted 28 July 2009 - 02:41 PM

Could you list a couple lines that are being returned even though they should not be returned? You could also try wrapping it in single quotes then:

grep '^[^a-fA-F]+$' filename

It should match lines that contain at least 1 character but does not contain any characters between a and f, case-insensitive. Blank lines are also ignored. There are several regex testers online. I usually use the javascript one:

http://www.regular-e...iptexample.html

or there's a site that offers different regex engines to test against:

http://www.regextester.com/
Was This Post Helpful? 0
  • +
  • -

#9 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Regex Trouble

Posted 28 July 2009 - 02:50 PM

View Postlanec42, on 28 Jul, 2009 - 02:33 PM, said:

Why not just [^a-f]+ ?


That matches all strings that contain at least one character that is not in the range of a-f. That's what I was trying to explain with your match on aardvark. If you use this regex, the 'r' will make this word a match. I'm under the impression you want to match all lines that do not have any a's,b's,c's,d's,e's, and f's. Maybe I misinterpreted and gave you the incorrect regex... If you want to look for all lines that do not begin with a-f, then change the regex to:

^[^a-fA-F]+

This will match all lines that contain at least one character and do not begin with a - f, case-insensitive. Meaning aardvark will fail, but "jam" will pass
Was This Post Helpful? 1
  • +
  • -

#10 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Regex Trouble

Posted 28 July 2009 - 02:51 PM

Nothing is being returned. The javascript regex tester in the your first link worked fine, but grep gives no matches.

egrep does, though! Looks like it's an extended regular expression. I still don't understand why the ^ and $ (beginning and end) are needed, but it works, so I'm happy :)

For the record:

me@here:~# egrep "^[^a-f]+$" 5desk | head
A
AB
ABC
ABCs
ABD
ABM
ACTH
AIDS
Ainu
Akihito


Woohoo :bananaman:

This post has been edited by lanec42: 28 July 2009 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1