View previous topic :: View next topic |
Author |
Message |
haimat Apprentice
Joined: 05 Sep 2002 Posts: 239 Location: Vienna / Austria
|
Posted: Thu May 08, 2003 1:00 pm Post subject: perl and regular expression |
|
|
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
but that doesn't work
What did I wrong - has anyone an idea? Greets and TIA, Matthias |
|
Back to top |
|
|
erebus n00b
Joined: 17 May 2002 Posts: 49 Location: United Kingdom
|
Posted: Thu May 08, 2003 2:11 pm Post subject: |
|
|
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 |
|
|
haimat Apprentice
Joined: 05 Sep 2002 Posts: 239 Location: Vienna / Austria
|
Posted: Thu May 08, 2003 2:29 pm Post subject: |
|
|
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
then I get all three lines... |
|
Back to top |
|
|
guero61 l33t
Joined: 14 Oct 2002 Posts: 811 Location: Behind you
|
Posted: Thu May 08, 2003 2:48 pm Post subject: |
|
|
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 |
|
|
haimat Apprentice
Joined: 05 Sep 2002 Posts: 239 Location: Vienna / Austria
|
Posted: Thu May 08, 2003 3:01 pm Post subject: |
|
|
ohh I tried it with
as well, but as you guess ... that doesn't work either |
|
Back to top |
|
|
Naan Yaar Bodhisattva
Joined: 27 Jun 2002 Posts: 1549
|
Posted: Thu May 08, 2003 3:23 pm Post subject: |
|
|
When I do:
Code: |
perl -e 'print grep /this\s+/, <>;' < test
|
where test is:
Code: |
this aaa
this2 aaa
this3 bbb
|
I get:
Seems to work fine. |
|
Back to top |
|
|
haimat Apprentice
Joined: 05 Sep 2002 Posts: 239 Location: Vienna / Austria
|
Posted: Thu May 08, 2003 4:02 pm Post subject: |
|
|
ok, seems I made a mistake. It works with
thx for all your input! Greets, Matthias |
|
Back to top |
|
|
dma Guru
Joined: 31 Jan 2003 Posts: 437 Location: Charlotte, NC, USA
|
Posted: Thu May 08, 2003 4:20 pm Post subject: |
|
|
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 |
|
|
ferringb Retired Dev
Joined: 03 Apr 2003 Posts: 357
|
Posted: Thu May 08, 2003 5:40 pm Post subject: |
|
|
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 |
|
|
guero61 l33t
Joined: 14 Oct 2002 Posts: 811 Location: Behind you
|
Posted: Thu May 08, 2003 11:00 pm Post subject: |
|
|
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 |
|
|
|