简体   繁体   English

使用Sed和Bash遍历Regexp比赛?

[英]Iterating Over Regexp Matches with Sed and Bash?

How can I iterate over multi-line regexp matches using sed and bash? 如何使用sed和bash遍历多行正则表达式匹配? I'm trying to generate some quick docs from comments in a file, like so: 我正在尝试根据文件中的注释生成一些快速文档,例如:

/**
 * @name myFun
 * @return {int}
 */

I can extract every comment block using sed -n -e '/\\/\\*\\*$/,/\\*\\/$/p' , but now I'd like to stuff each match into a bash array so I can parse the details later. 我可以使用sed -n -e '/\\/\\*\\*$/,/\\*\\/$/p'提取每个注释块,但是现在我想将每个匹配项填充到bash数组中,这样我就可以稍后解析细节。 Thanks. 谢谢。

I think you'd probably be better off using Doxygen . 我认为使用Doxygen可能会更好。

By the way, your sed command might be more readable using alternate delimiters: 顺便说一句,使用备用定界符,您的sed命令可能更具可读性:

sed -n -e '\|/\*\*$|,\|\*/$|p'

I think I would be looking to a scripting tool - I'd reach for Perl, but Python can probably handle it too. 我想我会使用脚本工具-我会接触Perl的,但是Python可能也可以处理它。 I'd probably slurp the entire source file, then use a multi-line regex to pick up the comments. 我可能会抓紧整个源文件,然后使用多行正则表达式来拾取注释。

The reason for not trying it in sed plus bash is that sed would output N-line comments, which you'd then have to split - and that would be tricky. 不使用sed plus bash尝试的原因是sed会输出N行注释,然后您必须将其拆分-这很棘手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM