【问题标题】:Trying to parse string values into an array after a pattern match尝试在模式匹配后将字符串值解析为数组
【发布时间】:2015-03-28 21:22:15
【问题描述】:

我在一个文本文件中有以下几行:

<Entry>
    <Key argument="ComputerNames"/>
    <Value type="string" argument="localhost,localhost,engine1,engine2"/></Entry>
<Entry>
    <Key argument="BranchIDMultiple"/>
    <Value type="int" argument="1"/></Entry>

我知道如何找到具有 ComputerNames 的行。我也知道如何阅读下一行。

我需要按如下方式解析参数数量可以是动态的行。解析输出应该是:

@result = $result[0]=localhost, $result[1]=localhost, $result[2]=engine1, $result[3]=engine2.    

必须至少有一个参数,但也可以有更多..

我无法构建正确的正则表达式来完成拆分。有什么想法吗?

【问题讨论】:

    标签: regex parsing match


    【解决方案1】:

    假设输入包含您的以下 xml 行。

    既然您提到您知道如何提取此行。我把那部分留给你了。

    得到这一行后,使用以下正则表达式

    String regex ="argument=\"[a-zA-Z0-9,]*\"" ;
    
    Pattern pattern = Pattern.compile(regex);
    
    Matcher matcher = pattern.matcher(input);
    String[] op;
    if(matcher.find())
     {
       op = input.subString(matcher.start(),matcher.end()).split(",");
     }
    

    【讨论】:

    • 您好!感谢您的代码示例。我不是 100% 了解以下内容:Pattern pattern = Pattern.compile(regex);匹配器 matcher = pattern.matcher(input);我看到我输入的文本行将是正确的输入?此外,正则表达式被编译到 pattern.matcher 中,因此 String{} 将包含拆分找到的尽可能多的值。如果我走上正轨,请告诉我。谢谢!
    • 我假设输入将只包含“" 这个值
    【解决方案2】:

    好的,这就是我所拥有的:

    --- 经过多次不同的尝试,我终于能够得到一些有用的东西。见下文:

    BEGIN { require 5.8.0; }
    
    use strict; 
    use warnings;
    
    # string to test regular expressions
    my $test_string = '<Value type="string" argument="400teets,localhost,localhost,engine1,engine2,engine50,engine100,100afdasfdas"/></Entry>';
    
    # print out the initial string
    print "The initial string is: $test_string\n\n";
    
    # first set of arguments - all words that have a comma after them
    my @first_words = ($test_string =~ /(\w+),/g);
    
    # print first set of arguments
    print "\nFirst set of arguments found\n";
    foreach my $word (@first_words) {
        print "$word\n";
    }
    
    # second set of arguments - all words that have a comma before them
    my @last_words = ($test_string =~ /,(\w+)/g);
    
    #print second set of arguments
    print "\nSecond set of arguments found\n";
    foreach my $word (@last_words) {
        print "$word\n";
    }
    
    #merge the sets by popping the last element off of last_words array and pushing it into the first_words array
    push(@first_words,pop(@last_words));
    
    #print the results
    print "\nMerged Sets\n";
    foreach my $word (@first_words) {
        print "$word\n";
    }
    
    # END OF PROGRAM
    

    --- 真的,如果你排除所有的打印语句和 cmets,你真正需要的就是这三行:

    my @first_words = ($test_string =~ /(\w+),/g);
    my @last_words = ($test_string =~ /,(\w+)/g);
    push(@first_words,pop(@last_words));
    

    --- 这里是输出:

    初始字符串为:

    找到第一组参数 400条 本地主机 本地主机 引擎1 引擎2 引擎50 引擎100

    找到第二组参数 本地主机 本地主机 引擎1 引擎2 引擎50 引擎100 100afdasfdas

    合并集 400条 本地主机 本地主机 引擎1 引擎2 引擎50 引擎100 100afdasfdas

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 2015-06-22
      • 1970-01-01
      • 2014-05-13
      • 2017-02-02
      相关资源
      最近更新 更多