Common wildcards and regular expressions misunderstandings

Where wildcards are applicable: In shell command lines or shell scripts, they are generally used to match file names.

Where regular expressions are applicable: When processing strings, there are generally general regular expressions and Perl regular expressions.

 

When I recently wrote a shell, I found that the understanding of regular and wildcards has always been biased. Let us know these basic symbols again.

1. Wildcards

 ﹡Match all: Match 0 or more of any characters.

  ? matches any single character

  
[ ! ] matches a character that is not within the brackets (similar to [ ^ ] in regular expressions, both are reverse selections)

When processing the above *,?,[ ] as parameters or strings, it is necessary to restrict the shell not to treat them as wildcards. The processing method at this time is: add single quotes or use \ (backslash escape)

 

2. Regular expressions

  

character match

. : matches any single character

*: matches any number of occurrences of the preceding character (distinguishes one or more characters from wildcards)

? : matches the preceding character 1 or 0 times

+ : matches at least one occurrence of the preceding character (in extended regular expressions)

location match

^: anchor the start of the line

$: anchors the end of the line

\< or \b: Anchor word start, any character after it must appear as word start

\> or \b: Anchor word ending, any character before it must appear as a word ending

\B: beginning or end of a non-word

^$: blank line

   

Use () for group matching:

(ac)* : Match the group ac any number of times

\1: Quote the first left parenthesis and all the contents of the corresponding right parenthesis, and similarly there are \2,\3

Assert match 

Java only supports the form <name>; in other languages, or in regular design, there is also the form (?"name")
zero-width assertion
<?=exp>
<?!=exp>
<?!<=exp>
<?<=exp>

^(.(?<!that))*this(.(?<!that))*$ 
^(.(?<!that))*this((?!that).)*$ 
^((?! that).)*this(.(?<!that))*$ 
^((?!that).)*this((?!that).)*$ 
These 4 regular expressions test the above sentences , the results can meet the requirements.

lazy mode
lazy qualifier
code/syntax illustrate
*? Repeat any number of times, but as little as possible
+? 1 or more repetitions, but as few repetitions as possible
?? 0 or 1 repetitions, but as few repetitions as possible
{n,m}? Repeat n to m times, but as few as possible
{n,}? Repeat more than n times, but as little as possible
lazy qualifier
grouping and backreferences
(?<name>.[a-z]*?)(/d)
You can use /2 in expressions

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324889449&siteId=291194637