What's the difference between * and .* in regular expressions?

自作多情 提交于 2020-01-23 17:37:08

问题


I saw the wildcard of * been used in some command like: find *.jpg, which means find any files ended with .jpg. However, in regular expressions, .* also means that 0 or more times for any character. So, what's the difference between them? When is * been used and when is .*?


回答1:


In regular expressions, * is the "zero or more" repetition marker. . is the "any single character (with caveats)" expression.

In a find the * character is an "anything" wildcard and is not really a regular expression character. The ? is the "any single character" in search situations of this kind.




回答2:


Filename matching patterns (or globs) and regular expressions are two similar but different things.

For both of them, most characters simply match themselves, and certain special characters have special meanings which can be suppressed by preceding them with a backslash \.

In a filename matching pattern (as used by shells and the find command), ? matches any single character and * matches zero or more arbitrary characters. A . (period) is not a special character; it just matches a literal . character.

In a regular expression (as used by grep, many text editors, and most commands that perform searches on text), . matches any single character and * denotes zero or more occurrences of whatever precedes it.

There's quite a bit more to it than that. Filename matching patterns have special rules for "hidden" files whose names start with ., and the syntax and rules can vary somewhat from one shell to another. Regular expressions also exist in several forms (grep vs. egrep, vs. Perl or PCRE, for example).



来源:https://stackoverflow.com/questions/19673427/whats-the-difference-between-and-in-regular-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!