Computing.Net > Forums > Programming > weird regex behavior

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

weird regex behavior

Reply to Message Icon

Original Message
Name: ShaqDiesel
Date: June 3, 2008 at 21:32:35 Pacific
Subject: weird regex behavior
OS: winXP
CPU/Ram: amd64/1Gb
Model/Manufacturer: custom
Comment:

echo "Here's \$7.80" |
grep -oE [$][0-9]+.[0-9]+

gives me my desired $7.80
But:
1. I had to escape the $7, so why:
a. CAN'T I escape the . in $7.80
b. IS IT OPTIONAL to escape the . in my
regular expression?

echo "this.that" | grep -o \. will not match the .
Why do I need to escape the backslash? I thought whenever you had a character you wanted to match literally you just needed to precede it with a backslash. The proper regex, which is \\. looks like I'm looking for a backslash followed by any single character

echo "this.that" | grep -o '.' interprets the special meaning of '.' If that's the case, why won't
echo "this.that" | grep -o '\\.' match exactly a dot?

Besides interpolating variables, is there a difference between using single quotes and double quotes? In Unix? In Kornshell?

Thanks.


Report Offensive Message For Removal


Response Number 1
Name: FishMonger
Date: June 3, 2008 at 21:58:15 Pacific
Reply: (edit)

Using the ' ' single quotes turned off shell expansion, which means that it was looking for the literal string of 2 backslashes followed by a dot.
========================================================
[root@perlman ~]# echo "this.that" | grep -o "\."
.


Report Offensive Follow Up For Removal

Response Number 2
Name: ShaqDiesel
Date: June 4, 2008 at 00:04:38 Pacific
Reply: (edit)

echo "this.that" | grep -o "\." To match the . why do I need to backslash escape the \?

And can you explain the dollar sign example I had? Thanks.


Report Offensive Follow Up For Removal

Response Number 3
Name: FishMonger
Date: June 4, 2008 at 01:03:19 Pacific
Reply: (edit)

Based on this and your other related questions, you should pick up a couple reference books.

Learning the Korn Shell, 2nd Edition
http://www.oreilly.com/catalog/korn...

Mastering Regular Expressions, Third Edition
http://www.oreilly.com/catalog/9780...


Report Offensive Follow Up For Removal

Response Number 4
Name: ShaqDiesel
Date: June 4, 2008 at 15:52:20 Pacific
Reply: (edit)

I'm actually using Learning the Kornshell lol it's not very good. It said to use the * like:
*(exp) for 0 or more exp. I tried that and it didn't work, but (exp)* does. As for Mastering Regular Expressions, doesn't that cover regular expressions in perl?


Report Offensive Follow Up For Removal

Response Number 5
Name: FishMonger
Date: June 4, 2008 at 16:04:56 Pacific
Reply: (edit)

As for Mastering Regular Expressions, doesn't that cover regular expressions in perl?

It covers details and differences between numerous languages...Perl, php, shell, java, etc.


Report Offensive Follow Up For Removal


Response Number 6
Name: FishMonger
Date: June 4, 2008 at 16:12:17 Pacific
Reply: (edit)

I'm actually using Learning the Kornshell lol it's not very good.

How about reading the man page, which is the main resource used in most shell scripting books.

man ksh
http://www.iti.cs.tu-bs.de/cgi-bin/...


Report Offensive Follow Up For Removal






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software