LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Standard wildcard and regex (https://www.linuxquestions.org/questions/linux-newbie-8/standard-wildcard-and-regex-4175596706/)

fanoflq 01-03-2017 10:39 AM

Standard wildcard and regex
 
There is standard wildcard (gobbling pattern) and regex.
Seems like the author said imply they are different.
See here:
http://www.tldp.org/LDP/GNU-Linux-To...tml/x11655.htm

What is the difference?

I tried these commands:
Code:

$ ls -a
.  ..  dir  dir1  .dotfile  .dotfile1  file1  file2  file3 

# Using ? like this does not work. Why?
 $ ls f?ile?
ls: cannot access 'f?ile?': No such file or directory

# But using ? like this works
 $ ls file?
file1  file2  file3

#Using .(dot) like this does not work
#How do you indicate dot is to be relace with any character?

 $ ls f.le?
ls: cannot access 'f.le?': No such file or directory

# Using .* does not work either.  Why?
 $ ls f.*le?
ls: cannot access 'f.*le?': No such file or directory

#But this works.
 $ ls f*le?
file1  file2  file3

Please advice.
Thank you & Happy New Year!.

hydrurga 01-03-2017 10:55 AM

These should help you: http://www.tldp.org/LDP/abs/html/globbingref.html and http://www.tldp.org/LDP/GNU-Linux-To...tml/x11655.htm

Note that a dot '.' is not one of the wildcard characters.

Note also that '?' represents a single character and can't represent no characters at all.

grail 01-03-2017 11:02 AM

You will need to supply more information as explanations for each example you have shown are answered under the wildcards section. Perhaps the issue is that none of your examples are regular expressions ...
are you aware of this? The bash shell, by default, will expand wildcards when used in conjunction with commands, in your case ls.

So to your examples:
Code:

$ ls f?ile? -- show all files containing the character 'f' followed by any character, followed by string 'ile', followed by any character ... none exist

$ ls file?  -- show all files starting with string 'file' followed by any character ... 3 exist as '?' can be 1, 2 or 3

$ ls f.le?  -- show all files starting with string 'f.le' followed by any character ... none exist, dot is in no files starting with an 'f'

$ ls f.*le? -- show all files starting with string 'f.' followed by any number of any character, followed by string 'le' followed by any character ... none exist, an example would be .. f.asd123leN

$ ls f*le?  -- show all files containing the character 'f' followed by any number of any character, followed by string 'le' followed by any character ... 3 exist as * will be replaced by 'i' in all examples
and '?' replaced by 1 - 3


fanoflq 01-03-2017 11:27 AM

Thanks folks.

This is a typo:
Quote:

$ ls f?ile?
It should be this:
Quote:

$ ls f?le?
file1 file2 file3
I now understood.
First, bash parse the command, but it does not use regex.
Bash use it own wildcard, i.e.globbing pattern.
See here http://www.tldp.org/LDP/abs/html/globbingref.html
Excerpts:" Bash does carry out filename expansion [1] -- a process known as globbing -- but this does not use the standard RE set. "

My mistake was also this:
dot is not a globbing pattern wildcard (which is different from regular regex).

Then bash executes the command with the expanded arguments that were not escaped.
The executed command use regular regex on the arguments, if there are any that escaped Bash's parsing.

Here is blessings for everyone:
Quote:

Let peace, let peace, let peace
fill your soul.

May peace, may peace, may peace
make you whole.

grail 01-03-2017 11:33 AM

Quote:

The executed command use regular regex on the arguments, if there are any that escaped Bash's parsing.
Not sure where you are going with the above statement, it is outright wrong when looking at a command like ls, but something like grep would use regular expressions. Any escaped item would also be escaped
for the regular expression unless the command involved does not use the same escape item.

The rest of your information appears correct.

fanoflq 01-03-2017 11:47 AM

Quote:

Originally Posted by grail (Post 5649745)
Not sure where you are going with the above statement, it is outright wrong when looking at a command like ls, but something like grep would use regular expressions. Any escaped item would also be escaped
for the regular expression unless the command involved does not use the same escape item.

The rest of your information appears correct.

Thank you for the clarification.
I should have mentioned that I was referring to quotes which prevents Bash from parsing the quoted arguments when used by commands like grep,.... etc.
Code:

$ echo f?le?
file1 file2 file3

$ echo "f?le?"
f?le?



All times are GMT -5. The time now is 03:41 AM.