Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
perl and regular expression
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
haimat
Apprentice
Apprentice


Joined: 05 Sep 2002
Posts: 239
Location: Vienna / Austria

PostPosted: Thu May 08, 2003 1:00 pm    Post subject: perl and regular expression Reply with quote

Hi all,

I have a small problem with perl and a RE. I have a file with this content:

Code:
test     12345
test2    3423423
test3    2342342


I want to extract the line with "test" and only "test" (so not "test2" and/or "test3"). I tried it with

Code:
grep "test\s+"

but that doesn't work :(
What did I wrong - has anyone an idea? Greets and TIA, Matthias
Back to top
View user's profile Send private message
erebus
n00b
n00b


Joined: 17 May 2002
Posts: 49
Location: United Kingdom

PostPosted: Thu May 08, 2003 2:11 pm    Post subject: Reply with quote

Hi ya,

Sorry about being a bit vague but I'm more or a tcl man when it comes to regular expressions... Anyway I had a quick go managed to get this bit of code to produce the rest you are looking for

[code]grep "test[[:space]]"[\code]

So I'm guessing that something is wrong with you're regular expression syntax's for a space...
Back to top
View user's profile Send private message
haimat
Apprentice
Apprentice


Joined: 05 Sep 2002
Posts: 239
Location: Vienna / Austria

PostPosted: Thu May 08, 2003 2:29 pm    Post subject: Reply with quote

hmm... it doesn't work even if I try it with

Code:
grep "test[\s]+"
grep "test[ ]+"
grep "test +"


with none of these :(

but if i do a

Code:
grep "test\s*"


then I get all three lines...
Back to top
View user's profile Send private message
guero61
l33t
l33t


Joined: 14 Oct 2002
Posts: 811
Location: Behind you

PostPosted: Thu May 08, 2003 2:48 pm    Post subject: Reply with quote

Is the whitespace always a space or can it be a tab (^I)? If you can count on it being a space, you can just put 'test ' and it'll find it for you.
Back to top
View user's profile Send private message
haimat
Apprentice
Apprentice


Joined: 05 Sep 2002
Posts: 239
Location: Vienna / Austria

PostPosted: Thu May 08, 2003 3:01 pm    Post subject: Reply with quote

ohh I tried it with

Code:
grep "test "


as well, but as you guess ... that doesn't work either :(
Back to top
View user's profile Send private message
Naan Yaar
Bodhisattva
Bodhisattva


Joined: 27 Jun 2002
Posts: 1549

PostPosted: Thu May 08, 2003 3:23 pm    Post subject: Reply with quote

When I do:
Code:

perl -e 'print grep /this\s+/, <>;' < test

where test is:
Code:

this            aaa
this2           aaa
this3           bbb


I get:
Code:

this            aaa


Seems to work fine.
Back to top
View user's profile Send private message
haimat
Apprentice
Apprentice


Joined: 05 Sep 2002
Posts: 239
Location: Vienna / Austria

PostPosted: Thu May 08, 2003 4:02 pm    Post subject: Reply with quote

ok, seems I made a mistake. It works with

Code:
grep "test "


thx for all your input! Greets, Matthias
Back to top
View user's profile Send private message
dma
Guru
Guru


Joined: 31 Jan 2003
Posts: 437
Location: Charlotte, NC, USA

PostPosted: Thu May 08, 2003 4:20 pm    Post subject: Reply with quote

Your problem is that you used doublequotes, within which the backslash has special meaning for the shell itself. Try using single quotes or putting a backslash before your backslash:

Code:
grep "test\\s+"
grep 'test\s+'
Back to top
View user's profile Send private message
ferringb
Retired Dev
Retired Dev


Joined: 03 Apr 2003
Posts: 357

PostPosted: Thu May 08, 2003 5:40 pm    Post subject: Reply with quote

Something nifty to use also- zero-width positive look ahead.
an example using you're data listed
...grep (/test(?=\s)/, @data)...

This will match test followed by whitespace, but nothing else. Take a look in the man page perlre for some of the other fun options (zero width negative look ahead- matching something that *isn't* followed by something).
Back to top
View user's profile Send private message
guero61
l33t
l33t


Joined: 14 Oct 2002
Posts: 811
Location: Behind you

PostPosted: Thu May 08, 2003 11:00 pm    Post subject: Reply with quote

dma wrote:
Your problem is that you used doublequotes, within which the backslash has special meaning for the shell itself. Try using single quotes or putting a backslash before your backslash:

Code:
grep "test\\s+"
grep 'test\s+'



The only problem is, he's not shelling out -- grep is a function in Perl that does the same action, just on an array or such instead of a file.

That said, I was kinda wondering why you weren't using /pattern/, but I figured you knew best -- I've had to mix /pattern/ and "pattern" as well as 'pattern' many times in Perl.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum