Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.txtFinally, 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.

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.

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.

You're comparing apples (shell expansion) to oranges (regular expression).
Regular Expressions are not the same as Shell Expansions.

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.

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.

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.

![]() |
can I "lock" a ...
|
weird regex behavior
|

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