给定字符串"fn=doAction&a=1&b=2",提取出fn=doAction&。
echo "fn=doAction&a=1&b=2" | grep -o "fn=.*&"
# fn=doAction&a=1&
输出‘fn=doAction&a=1&’,由于grep的匹配是贪婪的,匹配了最长的&。
改为
echo "fn=doAction&a=1&b=2" | grep -o -P "fn=.*?&"
# fn=doAction&
-P参数使用perl模式正则,*后面加个?使用non-greedy模式,搞定。
https://stackoverflow.com/questions/3027518/how-to-do-a-non-greedy-match-in-grep#3027524