简体   繁体   中英

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. $1 is a reference to something that is captured by ([0-9]+), so it is reference to some number if the matching exists. Is it possible to do something like that in grep?

Let's say, some xml document contains the following :

<someTag>someValue</someTag>

I would like to extract only someValue , but input for second_bash_script for the following:

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

is someValue . Is it possible to extract only someValue using grep?

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.

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

The second thing can be done with grep using Perl regexp:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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