我想用正则表达式和回显匹配来匹配字符串。
if [[ "${PLUGS}" =~ \"[a-zA-Z0-9.-]+ ]]; then
echo ${BASH_REMATCH[*]}
else
echo "nothing"
fi
但我不知道如何添加全局标志。如果我像 /\"[a-zA-Z0-9.-]+/g
这样写正则表达式,我只会得到第一个匹配项。我做错了什么?
编辑:
示例数据是 mysql 返回的 wordpress 事件插件 SELECT * FROM wp_options WHERE option_name = 'active_plugins';
a:31:{i:0;s:13:"AddMySite.php";i:1;s:19:"akismet/akismet.php";i:2;s:23:"all_in_one_seo_pack.php";i:3;s:16:"authenticate.php";i:4;s:28:"breadcrumb-navigation-xt.php";i:5;s:18:"codeautoescape.php";i:6;s:37:"contact-coldform/contact_coldform.php";i:7;s:32:"custom-query-string-reloaded.php";i:8;s:30:"customizable-post-listings.php";i:9;s:33:"dd-sitemap-gen/dd-sitemap-gen.php";i:10;s:20:"download-counter.php";i:11;s:13:"feedcount.php";i:12;s:13:"full_feed.php";i:13;s:15:"get-weather.php";i:14;s:36:"google-sitemap-generator/sitemap.php";i:15;s:13:"gravatars.php";i:16;s:19:"kill-admin-nags.php";i:17;s:18:"landingsites13.php";i:18;s:30:"nofollow-free/nofollowfree.php";i:19;s:17:"ol_feedburner.php";i:20;s:16:"plugins-used.php";i:21;s:22:"popularity-contest.php";i:22;s:39:"search-everything/search_everything.php";i:23;s:27:"simple-tags/simple-tags.php";i:24;s:26:"simple_recent_comments.php";i:25;s:18:"simple_twitter.php";i:26;s:25:"subscribe-to-comments.php";i:27;s:24:"the-excerpt-reloaded.php";i:28;s:18:"theme-switcher.php";i:29;s:9:"top10.php";i:30;s:16:"wp-db-backup.php";}
最佳答案
如果您只想提取所有匹配项并回显它们,请考虑使用 grep
和 -o
/--only-matching
标志:
$ grep -oE '("[a-zA-Z0-9.-]+")' <<<"${PLUGS}"
"AddMySite.php"
"authenticate.php"
"breadcrumb-navigation-xt.php"
"codeautoescape.php"
"custom-query-string-reloaded.php"
"customizable-post-listings.php"
"download-counter.php"
"feedcount.php"
"get-weather.php"
"gravatars.php"
"kill-admin-nags.php"
"landingsites13.php"
"plugins-used.php"
"popularity-contest.php"
"subscribe-to-comments.php"
"the-excerpt-reloaded.php"
"theme-switcher.php"
"top10.php"
"wp-db-backup.php"
我们还需要使用 -E
/--extended-regexp
,因为你的正则表达式是 POSIX ERE。
或者,也可以处理不匹配的情况(如您的问题):
if ! grep -o -E '"([a-zA-Z0-9.-]+)"' <<<"${PLUGS}"; then
echo "nothing"
fi
关于Linux bash 正则表达式全局标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44970561/