Bash Regex多个可选捕获

Bash Regex多个可选捕获,regex,wordpress,bash,Regex,Wordpress,Bash,我试图从wordpress插件中获取信息,插件名称和版本 我发现我要查找的行可能是插件名称:,插件名称:,插件名称:,或者*插件名称:,或者空间的变体。因为我要找的行在评论部分,所以它可能还包括* 我想,我已经尽可能地选择了空格,但我不知道如何用空格来捕获*的内容 grep "^\s*Plugin Name:" $files 这对大多数插件都有效,但并非所有插件都有效,因为有些插件需要注意其他字符 我试过了 (\s*)?|( \* )? [ *]|[ \* ]? ( *| \* )?

我试图从wordpress插件中获取信息,插件名称和版本

我发现我要查找的行可能是
插件名称:
插件名称:
插件名称:
,或者
*插件名称:
,或者空间的变体。因为我要找的行在评论部分,所以它可能还包括*

我想,我已经尽可能地选择了空格,但我不知道如何用空格来捕获*的内容

grep "^\s*Plugin Name:" $files
这对大多数插件都有效,但并非所有插件都有效,因为有些插件需要注意其他字符

我试过了

(\s*)?|( \* )?  
[ *]|[ \* ]? 
( *| \* )?
但是我很难把我的头绕在一起

编辑: 使用

将找到所有内容,但在本例中,现在有两个插件具有
*插件名称:
除了正确的信息外,还将打印出一个完全不同的目录

$plugPath是wordpress插件文件夹-/wp content/plugins

folders=`find $plugPath -maxdepth 1 -type d`
for plugin_folder in $folders
do
    echo $plugin_folder
    files=`find $plugin_folder -maxdepth 1 -name "*.php"`

    n=`grep -hE '^\s*\*?\s*Plugin Name:' $files`
    v=`grep -hE '^\s*\*?\s*Version:' $files`
    echo " -- "$n
    echo " -- "$v
done
我期待的结果是:

/home/test/accounts/account-one/public_html/wp-content/plugins/flexible-recent-posts
 -- Plugin Name: Flexible Recent Posts
 -- Version: 1.0.4
正在工作的示例:

Plugin Name: Flexible Recent Posts
Plugin URI: http://steelrat.info/
Description: Displays recent posts using flexible template system.
Version: 1.0.4
Author: SteelRat
在哪里搞砸了:

/home/test/accounts/account-one/public_html/wp-content/plugins/portfolio-post-type
 --  2015.11.13-AccountInfo.log.csv accounts bin Desktop Documents Downloads latest.tar.gz Music Pictures Public Templates test-info.log.csv Videos wordpress Plugin Name: Portfolio Post Type
 --  2015.11.13-AccountInfo.log.csv accounts bin Desktop Documents Downloads latest.tar.gz Music Pictures Public Templates test-info.log.csv Videos wordpress Version: 0.9.1
它在其中找到的文件如下所示:

 * Plugin Name: Portfolio Post Type
 * Plugin URI:  http://wptheming.com/portfolio-post-type/
 * Description: Enables a portfolio post type and taxonomies.
 * Version:     0.9.1
一个简单的例子:

grep "Plugin Name:"
这还不够吗

更新

然后尝试以下版本:

grep -E '^\s*[*]?\s*Plugin Name:'
或同等标准:

grep -E '^\s*\*?\s*Plugin Name:'
更新2

更新问题中的循环可能会出现未充分引用文件名综合症。我建议使用find one liner来获得几乎相同的功能:

find "${WORDPRESS_PLUGIN_DIR}" -mindepth 2 -maxdepth 2 -iname "*.php" -exec grep -E '^\s*\*?\s*(Plugin Name|Version):' {} \;

这将捕获所有内容,包括前面的空格,再加上过去的单个空格
插件名称:

grep -G "^.*Plugin Name:\s" $files

你不能只使用
^\s*\*\s*插件名:
?有什么问题吗?顺便问一下,如果你的
*
必须用空格包装:
^\s*\(\*\)\?\s*
。因为我在一些插件上得到了这样的回报:资产功能grunt Grunfile.js info.txt语言\u modd README.md rebrand.gif插件名称:Portfolio PostType@ooklah:我理解,尝试更新版本的regextwo插件,其中包含*仍然包括第一条评论中的所有额外内容,但它确实包含基本上包括正确的信息。哪一个比我刚开始的更好empty@ooklah:更新问题报告有问题的行,我将修复正则表达式以删除它们我更新了问题,似乎当它发现一个插件中有*时,它将抓取它所在的主目录或其他插件之一并添加它当我重新打开终端时,它不再显示插件,而是显示我的主目录。
grep -G "^.*Plugin Name:\s" $files