Computing.Net > Forums > Programming > weird regex behavior

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

weird regex behavior

Reply to Message Icon

Name: ShaqDiesel
Date: June 3, 2008 at 21:32:35 Pacific
OS: winXP
CPU/Ram: amd64/1Gb
Product: 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.



Sponsored Link
Ads by Google

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

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 "\."
.


0

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

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.


0

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

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...


0

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

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?


0

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

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.


0

Related Posts

See More



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

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/...


0

Sponsored Link
Ads by Google
Reply to Message Icon






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


Sponsored links

Ads by Google


Results for: weird regex behavior

[JAVA] group reference of regex www.computing.net/answers/programming/java-group-reference-of-regex/3589.html

perl regex www.computing.net/answers/programming/perl-regex/14563.html

VB; Regex class can't be used www.computing.net/answers/programming/vb-regex-class-cant-be-used/15661.html