在首次出现字符串后使用Regex捕获内容

时间:2016-03-25 14:47:10

标签: regex perl

我做了一些研究,我正在努力弄清楚如何回答这个问题。我有以下文字,我想在商家地址栏中提取邮政编码:

BUSINESS ADDRESS:   
    STREET 1:       101 AWESOME DRIVE
    STREET 2:       P O BOX 144
    CITY:           HOUSTON
    STATE:          TX
    ZIP:            77027
    BUSINESS PHONE: 7138675309

MAIL ADDRESS:   
    STREET 1:       P O BOX 144
    CITY:           HOUSTON
    STATE:          TX
    ZIP:            77001

此代码捕获最后一个实例(77001):

(BUSINESS\s*ADDRESS:)(.*)(ZIP:\s*)(.*)

如何捕获第一个邮政编码(77027)?

感谢您帮助一个菜鸟。

4 个答案:

答案 0 :(得分:2)

嗯,在您的示例中,您只需要将问号添加到(。*?)并指定zip只包含数字:

BUSINESS\s*ADDRESS:.*?ZIP:\s*(\d+)

默认情况下,星号和加号是贪婪的。 而且无需捕获邮政编码以外的其他内容

答案 1 :(得分:2)

假设:

my $tgt="BUSINESS ADDRESS:   
    STREET 1:       101 AWESOME DRIVE
    STREET 2:       P O BOX 144
    CITY:           HOUSTON
    STATE:          TX
    ZIP:            77027
    BUSINESS PHONE: 7138675309

MAIL ADDRESS:   
    STREET 1:       P O BOX 144
    CITY:           HOUSTON
    STATE:          TX
    ZIP:            77001";

你可以这样做:

print "$1: $2\n" while $tgt=~/^(\S[^:]+):[^\R]*\R.*?^\s+ZIP:\s+(\d+)/gms;

打印:

BUSINESS ADDRESS: 77027
MAIL ADDRESS: 77001

同样的方法,您可以构造一个哈希映射,将地址映射到每个块的zip。

答案 2 :(得分:1)

对于那些即将上映......

下面有一个经过测试的版本,因为该文件在当前目录中名为test.txt:

awk '{if ($0 ~ /BUSINESS ADDRESS:/) { inzone=1; } if (inzone) {if ($0 ~ /ZIP:/) { print $2; } else if ($0 ~ /MAIL ADDRESS:/) { inzone=0; }}}' test.txt

它将打印包含 ZIP:的所有行的第二个字段,但仅打印包含BUSINESS ADDRESS的行与包含MAIL ADDRESS的另一行之间的块中遇到的行:

测试如下:

awk '{if ($0 ~ /BUSINESS ADDRESS:/) { inzone=1; } if (inzone) {if ($0 ~ /ZIP:/) { print $2; } else if ($0 ~ /MAIL ADDRESS:/) { inzone=0; }}}' test.txt
77027

答案 3 :(得分:1)

在列表上下文中运行的匹配运算符将返回找到的所有匹配值。所以你可以这样做:

$(document).ready(function () {
    //apply the value on page ready (you probably don't have to wait until everyithing loads - otherwise chage it to window.load()
    $('#selectedvalue').html( $('input[name=radiobuttons]:checked').val() );

    //bind the change event to update the element
    $('input[name=radiobuttons]').on('change', function () {
        $('#selectedvalue').html( $(this).val() );
    });
});

打印哪些:

my $data = '
BUSINESS ADDRESS:   
    STREET 1:       101 AWESOME DRIVE
    STREET 2:       P O BOX 144
    CITY:           HOUSTON
    STATE:          TX
    ZIP:            77027
    BUSINESS PHONE: 7138675309

MAIL ADDRESS:   
    STREET 1:       P O BOX 144
    CITY:           HOUSTON
    STATE:          TX
    ZIP:            77001
';

my @allzips = ($data =~ /ZIP:\s*(\d+)/g);

foreach my $zip (@allzips) {   
    print "Found ZIP: $zip\n";
}