正则表达式在 if 语句中匹配带有空格的字符串(使用引号?) [英] Regex match a string with spaces (use quotes?) in an if statement
本文介绍了正则表达式在 if 语句中匹配带有空格的字符串(使用引号?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将如何进行如下所示的正则表达式匹配,但在 ("^This") 周围加上引号,因为在现实世界中,This"将是一个可以包含空格的字符串.
How would I do a regex match as shown below but with quotes around the ("^This") as in the real world "This" will be a string that can have spaces in it.
#!/bin/bash
text="This is just a test string"
if [[ "$text" =~ ^This ]]; then
echo "matched"
else
echo "not matched"
fi
我想做类似的事情
if [[ "$text" =~ "^This is" ]]; then
但这不匹配.
推荐答案
可以在空格前使用.
#!/bin/bash
text="This is just a test string"
if [[ "$text" =~ ^This is just ]]; then
echo "matched"
else
echo "not matched"
fi
这篇关于正则表达式在 if 语句中匹配带有空格的字符串(使用引号?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文