需要正则表达式的下列表达式?

时间:2016-09-12 06:39:03

标签: regex perl

#! /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_xxxxx_xxxx_xxxxxx_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`。请帮助创建一个正则表达式。

2 个答案:

答案 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;