#! /usr/bin/perl
$str = "ab_cde,efg_gh,drg_fgt,main_xx,sum(abc),avg(def)";
或
$str = "ab_cde,bc_bn,gy_ihf,efg_gh,drg_fgt,main_xx,sum(abc),avg(def)";
伙计们,main_xx
之前的字符串是动态的,意味着可以有更多使用此格式的元素,例如xx_xx
或xxx_xx
或xx_xxx
或xxx_xxxx
或它可以是“下划线”之前和之后的多个字符。因此,在main_xx
之前,可以使用上述格式的许多元素。我想匹配字符串UP TO main_xx because even fetching dynamically, this "main_xx" will be the last element and want to ignore elements after
main_xx`。请帮助创建一个正则表达式。
答案 0 :(得分:1)
#!/usr/bin/perl -w
use strict;
my $str = "ab_cde,efg_gh,drg_fgt,main_xx,sum(abc),avg(def)";
(my $result) = ($str =~ m/(.*main_xx)/);
print $result;
输出将是main_xx
之前的所有内容(给定xx
只是由x组成的字符串)。
答案 1 :(得分:1)
试试这个
my $str = "ab_cde,efg_gh,drg_fgt,main_xx,sum(abc),avg(def)";
my ($match)= $str =~m/(.+main_xx)/;
print $match;