Git bash 在 Windows 上的正则表达式结果与 CentOS 上的终端不同
Git bash on Windows different result than terminal on CentOS for regex
查看以下cleanCustomer.sh
文件
#!/bin/bash
customer=Reportçós
cleanedCustomer=${customer//[^a-zA-Z0-9 \-_.]/}
echo $cleanedCustomer
当我 运行 它在 Git Windows 11 上 Bash 它打印 Reports
.
当我在终端的 CentOS 上 运行 它打印 Reportçós
.
有人知道为什么 a-z
在 CentOS 中被解释为字母字符而不是在 Windows 中吗?
如何确保 CentOS 只考虑英文字符?
来自bash manual:
A pair of characters separated by a hyphen denotes a range expression; any character that falls between those two characters, inclusive, using the current locale’s collating sequence and character set, is matched. If the first character following the ‘[’ is a ‘!’ or a ‘^’ then any character not enclosed is matched. A ‘-’ may be matched by including it as the first or last character in the set.
您的 Git Bash 语言环境使用的规则不匹配 a-z
范围内的重音字符,而您的 CentOS 语言环境会。这可以通过使用一致的语言环境(如 C
进行整理)来解决。另外你的 -
放错地方了;它必须是第一个或最后一个,反斜杠需要用另一个反斜杠进行转义以匹配文字。
#!/bin/bash
LC_COLLATE=C
customer=Reportçós
cleanedCustomer=${customer//[^a-zA-Z0-9 \_.-]/}
printf "%s\n" "$cleanedCustomer"
查看以下cleanCustomer.sh
文件
#!/bin/bash
customer=Reportçós
cleanedCustomer=${customer//[^a-zA-Z0-9 \-_.]/}
echo $cleanedCustomer
当我 运行 它在 Git Windows 11 上 Bash 它打印 Reports
.
当我在终端的 CentOS 上 运行 它打印 Reportçós
.
有人知道为什么 a-z
在 CentOS 中被解释为字母字符而不是在 Windows 中吗?
如何确保 CentOS 只考虑英文字符?
来自bash manual:
A pair of characters separated by a hyphen denotes a range expression; any character that falls between those two characters, inclusive, using the current locale’s collating sequence and character set, is matched. If the first character following the ‘[’ is a ‘!’ or a ‘^’ then any character not enclosed is matched. A ‘-’ may be matched by including it as the first or last character in the set.
您的 Git Bash 语言环境使用的规则不匹配 a-z
范围内的重音字符,而您的 CentOS 语言环境会。这可以通过使用一致的语言环境(如 C
进行整理)来解决。另外你的 -
放错地方了;它必须是第一个或最后一个,反斜杠需要用另一个反斜杠进行转义以匹配文字。
#!/bin/bash
LC_COLLATE=C
customer=Reportçós
cleanedCustomer=${customer//[^a-zA-Z0-9 \_.-]/}
printf "%s\n" "$cleanedCustomer"