Regular Expressions to match files with speecial f

2019-09-06 11:23发布

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

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

标签: grep
2条回答
不美不萌又怎样
2楼-- · 2019-09-06 11:56

Try this:

^file(100|[1-9]\d|\d)(\.(100|[1-9]\d|\d))?(bla)*$
查看更多
倾城 Initia
3楼-- · 2019-09-06 11:59

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.

查看更多
登录 后发表回答