[英]RegEx - removing preceding & trailing whitespace while matching contents
I want to try and construct a RegEx statement that will remove any pre-ceeding or trailing white spaces from a string (but leave any contained within the string untouched) while also matching a chosen format. 我想尝试构建一个RegEx语句,它将从字符串中删除任何预先停止或尾随的空格(但不保留字符串中包含的任何内容),同时还匹配所选格式。 For instance the string must cannot be any longer than 20 characters, can contain any of the characters from a-zA-Z0-9 as well as underscores and hyphens. 例如,字符串不能超过20个字符,可以包含a-zA-Z0-9中的任何字符以及下划线和连字符。 But most importantly it must trim or ignore any spaces found at either the start or end of the string so: 但最重要的是,它必须修剪或忽略在字符串的开头或结尾处找到的任何空格,以便:
Correct: "Have a nice day" 正确:“祝你有个美好的一天”
Incorrect: " Have a nice day to " 不正确:“度过愉快的一天”
I have tried many different ways of doing this but unfortunately so far I have not been able to come up with a formula that does exactly what I want. 我尝试了许多不同的方法,但不幸的是到目前为止,我还没有能够提出一个完全符合我想要的公式。 Could anyone help me with a suitable RegEx? 谁能帮助我使用合适的RegEx? (this is a RegEx in its simplest form and not platform specific). (这是最简单形式的RegEx,而不是特定于平台的)。
Search for this pattern: 搜索此模式:
^\s*(.*?)\s*$
Replace with this one: 替换为这一个:
\1
It seems from your example that space is allowed in the middle of the string as well, so try this one 从你的例子看来,字符串中间也允许使用空格,所以试试这个
^((([a-zA-Z0-9\-_][a-zA-Z0-9\-_ ]{0,18}?[a-zA-Z0-9\-_]))|([a-zA-Z0-9\-_]))$
Anything which is matched is correct. 任何匹配的都是正确的。
The or
section in the pattern is used for supporting a single character sentence. 模式中的or
部分用于支持单个字符句子。
This one should suit your needs: 这个应该适合您的需求:
^\S[\w\s-]{0,18}\S$
18
is the maxlength - 2
of the string, because of the two \\S
that will match any non whitespace char each. 18
是字符串的maxlength - 2
,因为两个\\S
将匹配任何非空白字符。 For example it this case, strings won't match if their length is higher than 20
chars. 例如,在这种情况下,如果字符串的长度高于20
字符,则字符串将不匹配。 Moreover, strings won't match if their length is lower than 2
chars, due to the same above constraint. 此外,由于相同的上述约束,如果长度低于2
字符,则字符串将不匹配。
use this regex ^\\S((.{0,18}\\S)|)$
使用这个正则表达式^\\S((.{0,18}\\S)|)$
^
begin of string ^
字符串的开头
\\S
non-space symbol \\S
非空间符号
(.{0,18}\\S)?
any symbol and non-space symbol at the end (0-19 symbols) 最后的任何符号和非空格符号(0-19个符号)
|
or 要么
$
end of string $
end of string
这是一个使用sed的例子
echo " Have a nice day "|sed -E "s/^ *([^ ]+.*[^ ]+) +$/\\1/"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.