简体   繁体   English

“expr match”正则表达式中的“空格”字符有什么特别之处?

[英]What's special about a “space” character in an “expr match” regexp?

In a bash shell, I set line like so: 在bash shell中,我设置如下行:

line="total active bytes:         256"

Now, I just want to get the digits from that line so I do: 现在,我只想从该行获取数字,所以我这样做:

echo $(expr match "$line" '.*\([[:digit:]]*\)' )

and I don't get anything. 我什么都没得到 But, if I add a space character before the first backslash in the regexp, then it works: 但是,如果我在regexp中的第一个反斜杠之前添加一个空格字符,那么它可以工作:

echo $(expr match "$line" '.* \([[:digit:]]*\)' )

Why? 为什么?

The space isn't special at all. 这个空间并不特别。 What's happening is that in the first case, the .* matches the entire string (ie, it matches "greedily"), including the numbers, and since you've quantified the digits with * (as opposed to \\+ ), that part of the regex is allowed to match 0 characters. 发生的事情是,在第一种情况下, .*匹配整个字符串(即,它匹配“贪婪”),包括数字,并且因为你用* (而不是\\+ )量化了数字,那部分正则表达式允许匹配0个字符。

By putting a space before the digit match, the first part can only match up to but not including the last space in the string, leaving the digits to be matched by \\([[:digit:]]*\\) . 通过在数字匹配之前放置一个空格,第一部分只能匹配但不包括字符串中的最后一个空格,使数字与\\([[:digit:]]*\\)匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM