LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need help with regular expression (https://www.linuxquestions.org/questions/programming-9/need-help-with-regular-expression-379484/)

aecaudel 11-03-2005 01:27 AM

Need help with regular expression
 
I am trying to use find (under linux) to match file names of the form <anything>_[0-9]. For example:

text_0.txt
.foo_19.bar.c
etc.

I am using:

find . -regex '^\.*_[0-9]+' -print

So far it will only match the filename if it is preceeded by a "."

What am I doing wrong?

fvgestel 11-03-2005 04:45 AM

the backslash is making the dot go literal. This should work :
Code:

find . -regex '_[0-9]+\.' -print

aecaudel 11-03-2005 01:59 PM

Nope, doesn't work.

Actually the "\" in my first post wasn't necessary. Even without it, the regex only finds filenames with an initial "." It's as if the dot is being escaped even without the slash.

schneidz 11-03-2005 02:34 PM

my aix version of 'find' doesn't like regex.

Code:

schneidz@lq:/temp> find . -regex '_[0-9]+\.' -print
find: 0652-017 -regex is not a valid option.

if you dont mind grep this does it for me:
Code:

schneidz@lq:/temp> find . | grep _[0-9]
./.temp_5.tmp
./dupe/schneidz_11-03-05-report2.tmp
./temp_51.tmp
./hello_2.tmp
./world_23.tmp

peace

aecaudel 11-03-2005 03:29 PM

Thanks, schneidz, that does it!

Although, for my own information, It would be instructive if someone could tell me why the -regex as above doesn't work.

bigearsbilly 11-04-2005 02:40 AM

have you heard of man ?
;)



Code:


      -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 are by default POSIX Basic Regular Expressions, but this
              can be changed with the -regextype option.


fvgestel 11-04-2005 05:28 AM

Yes, I also noticed that the whole path is matched. Not only that, find adds a carot at the start and a dollar sign at the end of the expression. So the following regexp SHOULD work :
Code:

find . -regex '.*_[0-9]+\.[^/]*'


All times are GMT -5. The time now is 07:37 PM.