2 Replies Latest reply: Jan 9, 2010 4:26 AM by 807580 RSS

    Is it possible in Java to catch match in regular expression?

    807580
      Is it possible in Java to catch the match in regular expression?
      In Perl I can catch what I match like this:
      $line =~ s/(\w*)\s(\w*)\s(\w*)\s(\w*)\s(\w*)\s(\w*)\s(\w*)/chords.put(\"$key\", new String[][]\{\{$1\},\{$2\},\{$3\},\{$4\},\{$5\},\{$6\},\{$7\}\});/;
      With $NUMBER I can get what I catch with brackets.
        • 1. Re: Is it possible in Java to catch match in regular expression?
          807580
          Hello sickk,

          take a look at java.util.regex.Pattern.group(int).

          EDIT: Matcher, not Pattern (though Pattern is interesting too).

          With kind regards
          Ben
          • 2. Re: Is it possible in Java to catch match in regular expression?
            807580
            sickk wrote:
            Is it possible in Java to catch the match in regular expression?
            In Perl I can catch what I match like this:
            $line =~ s/(\w*)\s(\w*)\s(\w*)\s(\w*)\s(\w*)\s(\w*)\s(\w*)/chords.put(\"$key\", new String[][]\{\{$1\},\{$2\},\{$3\},\{$4\},\{$5\},\{$6\},\{$7\}\});/;
            With $NUMBER I can get what I catch with brackets.
            Yes, you can do it with String.replaceAll();
            C:\sickk>echo "a b c d e f g" | perl sickk.pl
            "chords.put("", new String[][]{{a},{b},{c},{d},{e},{f},{g}});"
            
            C:\sickk>java Sickk "a b c d e f g"
            chords.put("$key", new String[][]{{a},{b},{c},{d},{e},{f},{g}});
            But java does not magically resolve "$key" for you.