Regex 无法理解如何使用正则表达式从头到尾修剪行中正则表达式中的空格

Regex 无法理解如何使用正则表达式从头到尾修剪行中正则表达式中的空格,regex,Regex,我不熟悉正则表达式。我试图理解在行中修剪空白的概念。在这个过程中,我遇到了这个片段 下面是一个例子: 这个问题也有了解决办法 ^\s*([\w\s.]*)\s*$ 然而,我发现这很难理解。让我解释一下我的理解直到现在 ^ - Starting of the line \s* - WhiteSpace [\w\s.]*- Combination of a word character and white space character??(Zero or mor

我不熟悉正则表达式。我试图理解在行中修剪空白的概念。在这个过程中,我遇到了这个片段

下面是一个例子:

这个问题也有了解决办法

 ^\s*([\w\s.]*)\s*$
然而,我发现这很难理解。让我解释一下我的理解直到现在

 ^   - Starting of the line
\s*  - WhiteSpace
[\w\s.]*- Combination of a word character and white space character??(Zero 
          or more times??)(Have not understood properly)
 \s*  -Again a whitespace character(Zero or more times?)
 $   -End of the line

我能够理解语法,但无法理解它的完整含义。任何帮助都将不胜感激。

\s*
,零个或更多的空格将匹配连续空格。为什么我们使用start而不是plus表示连续空格可能存在,也可能不存在。同样,这也适用于字符类
[\w\s.]*
,它可能与单词char、空格或点零次或多次匹配,它将匹配
foo bar.buzz
,因为所有字母数字字符加下划线都被视为单词字符,由
\w
匹配,中间的空格由
\s
匹配,最后一个点由
匹配
[\w\s.]*
重复模式组合
\w
\s
零次或多次。请注意,
[\w\s.]
仅与单个字符匹配


看起来很对。我会尽力澄清

 ^       - Start of the line
\s       - Single whitespace character (space or tab or line return)
*        - 0 or more of the previous item
\s*      - Zero or more whitespace characters
[]       - Character class. Matches any of the characters or groups of characters
           contained
[\w\s.]  - Matches a word character, a whitespace character or a period ('.')
[\w\s.]* - Matches 0 or more of the above
 $       - End of the line