Computing.Net > Forums > Programming > Unix Regular Expressions

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.

Unix Regular Expressions

Reply to Message Icon

Name: ShaqDiesel
Date: June 3, 2008 at 20:08:32 Pacific
OS: winXP
CPU/Ram: amd64/1Gb
Product: custom
Comment:

I have noticed that regex in perl are very different than regex in unix. For example, perl* in perl matches strings containing per followed by 0 or more l's. In Unix, it matches any string starting with perl.

So what's the deal with regex in Unix, are they the same as regex for the shell you are using?

I have a file called xTest.txt
I ran find -name "Test.txt" and surprisingly it didn't find it. In perl, it would have found it because Test.txt is a substring of xTest.txt

Finally, I read that regex by default do greedy matching. However, I tried the following in Unix: grep -o aster* textFile.txt

textFile.txt contains the line
"asterisks by themselves in unix mean any number of any characters".

The -o option lets you see what string matched the regex, and it only returned 'aster', which implies unix actually does NON-greedy matching, unlike perl.



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: June 3, 2008 at 21:13:52 Pacific
Reply:

For example, perl* in perl matches strings containing per followed by 0 or more l's. In Unix, it matches any string starting with perl.
Where in UNIX? Which UNIX? Under what conditions?

I ran find -name "Test.txt" and surprisingly it didn't find it.
find does not use regular expressions.

it only returned 'aster', which implies unix actually does NON-greedy matching, unlike perl.
Make sure your version of grep does not need an argument to use regular expressions. Also, different versions of grep give you different behavior.


0

Response Number 2
Name: ShaqDiesel
Date: June 3, 2008 at 21:21:53 Pacific
Reply:

actually, find does use regular expressions.

For example, if you want to find all text files from your home directory, type:

find ~ -name "*.txt" -type f

I don't think a huge matter like greedy versus non greedy would change in versions of grep.


0

Response Number 3
Name: FishMonger
Date: June 3, 2008 at 21:26:28 Pacific
Reply:

You're comparing apples (shell expansion) to oranges (regular expression).

Regular Expressions are not the same as Shell Expansions.


0

Response Number 4
Name: FishMonger
Date: June 3, 2008 at 21:31:17 Pacific
Reply:

find ~ -name "*.txt" -type f

That's using shell expansion, not regular expression.

To use a regex in a grep command, you need to use the proper option -e -E -P
==========================================================
-E, --extended-regexp
Interpret PATTERN as an extended regular expression (see below).

-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern; useful to protect patterns beginning with -.

-P, --perl-regexp
Interpret PATTERN as a Perl regular expression.


0

Response Number 5
Name: FishMonger
Date: June 3, 2008 at 21:35:03 Pacific
Reply:

Here are the regex options for the find command on my fedora box.
=====================================================
-iregex pattern
Like -regex, but the match is case insensitive.

-regex pattern
File name matches regular expression pattern. This is a match on the whole path, not a search. For example, to match a file
named './fubar3', you can use the regular expression '.*bar.' or '.*b.*3', but not 'f.*r3'. The regular expressions understood
by find follow the conventions for the re_match system library function where this is present (i.e. on systems using the GNU C
Library). On other systems, the implementation within Gnulib is used; by default, Gnulib provides ''basic'' regular expres-
sions.


0

Related Posts

See More



Response Number 6
Name: ShaqDiesel
Date: June 3, 2008 at 21:37:31 Pacific
Reply:

what's shell expansion? how is it different from regular expressions? I'm really confused now...


0

Response Number 7
Name: FishMonger
Date: June 3, 2008 at 22:04:00 Pacific
Reply:

Shell expansion:
http://tldp.org/LDP/Bash-Beginners-...


0

Response Number 8
Name: ShaqDiesel
Date: June 4, 2008 at 00:09:33 Pacific
Reply:

I looked through that article after I googled shell expansion, but it didn't explain the * and how it is different than a regex. The article is also in bash, and I'm working in kornshell.


0

Sponsored Link
Ads by Google
Reply to Message Icon

can I "lock" a ... weird regex behavior



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: Unix Regular Expressions

about Regular expression www.computing.net/answers/programming/about-regular-expression/5935.html

Perl regular expression www.computing.net/answers/programming/perl-regular-expression/12645.html

regular expressions www.computing.net/answers/programming/regular-expressions-/10445.html