Regular Expressions to match files with speecial format
I have the following names of files: When x is a number (x could be number between 0 to 100)
fileX
fileXblabla
fileX.X
fileX.Xblabla
For example
fileX --> file1
fileXblabla --> file1blabla
fileX.X --> file5.3
fileX.Xblabla --> file2.9blabla
my question is how to match each one of the following kind of files under directory (by ls and Regular Expressions or something else)
For example
fileX
Under directory we want to get all files like: file1 or file2 or file3 …..file100
fileXblabla
Under directory we want to get all files like: file1bla or file2bla or file3bla …..file100bla
fil开发者_开发问答eX.X
Under directory we want to get all files like: file1.1 or file2.1 or file3.1 …..file100.100
fileX.Xblabla
Under directory we want to get all files like: file1.1bla or file2.1bla or file3.1bla …..file100.100bla
Try this:
^file(100|[1-9]\d|\d)(\.(100|[1-9]\d|\d))?(bla)*$
The following REGEX will match all 4 patterns:
file(\d{1,3})(\w*)(?:\.(\d{0,3})(\w*))?
The X in fileX will be in back reference 1
The blabla in fileXblabla will be in back reference 2
The Y in fileX.Y will be in back reference 3
The blabla in fileX.Yblabla be in back reference 4.
If you need any more help just raise add a comment.
精彩评论