简体   繁体   English

在grep中寻找mod_rewrite正则表达式功能

[英]Looking for mod_rewrite regular expression feature in grep

Because picture (or example) is worth more than thousand words, I'll use an example: 因为图片(或示例)价值超过一千个单词,所以我将使用一个示例:

RewriteRule ^/products/([0-9]+)$ /content.php?id=$1

In this RewriteRule example we've got simple regular expression. 在这个RewriteRule示例中,我们有简单的正则表达式。 $1 is a reference to something that is captured by ([0-9]+), so it is reference to some number if the matching exists. $ 1是对([0-9] +)所捕获内容的引用,因此,如果存在匹配项,则引用某个数字。 Is it possible to do something like that in grep? 是否可以在grep中执行类似的操作?

Let's say, some xml document contains the following : 假设某些xml文档包含以下内容:

<someTag>someValue</someTag>

I would like to extract only someValue , but input for second_bash_script for the following: 我只想提取someValue ,但要为second_bash_script输入以下内容:

first_bash_script | grep "<someTag>\([[:digit::]]\)\+</someTag>" | second_bash_script

is someValue . someValue Is it possible to extract only someValue using grep? 是否可以使用grep仅提取someValue

Thanks for any clue! 感谢您提供任何线索!

Those are two separate questions, right? 那是两个独立的问题,对吗?

The answer to the first one would be: use sed , grep doesn't do substitutions. 第一个答案是:使用sedgrep不做替换。

sed 's_^/products/\([0-9]\+\)_/content.php?id=\1_g'

The second thing can be done with grep using Perl regexp: 第二件事可以使用Perl regexp使用grep完成:

$ echo '<someTag>42</someTag>' | grep -oP '(?<=<someTag>)\d+(?=</someTag>)'
42

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

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