开发者

Why does this bash/sed call work?

I've been looking at examples of using sed to extract a substring using regex and I have a test script working. Problem is I don't understand why and would like to. Here's the script:

#!/bin/bash

string="       ID  : s0016b54e23bc.ab.cd.efghig\
Name : cd167095"
echo -e "string: '$string'"
name=`echo $string | sed 's/.*\(cd.*\)/\1/'`
echo -e "\nExtracted: $name"

And it outputs:

string: '       ID  : s0016b54e23bc.ab.cd.efghigName : cd167095'

Extracted: cd167095

The regex should have two matches:

cd.efghigName : cd167095 

and

cd167095
开发者_StackOverflow

Why is the second match returned?


Because it's "greedy"

The first .* matches as much as possible for the expression as a whole to succeed.

To see this, change the second cd to ef or something, and you will see the script return the first.

Now, if you use something like Ruby, Python, or Perl, you get more elaborate regular expressions, and you can use .*? which is the "non-greedy" form of .*.

#!/usr/bin/env ruby

string="       ID  : s0016b54e23bc.ab.cd.efghig\
Name : cd167095"
puts string.gsub /.*?(cd.*)/, '\1'
so ross$ ./qq3
cd.efghigName : cd167095

Though really, I would just write:

string[/cd.*/]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜