org.knowceans.util
Class NamedGroupsRegex

java.lang.Object
  extended by org.knowceans.util.NamedGroupsRegex

public class NamedGroupsRegex
extends java.lang.Object

NamedGroupRegex allows to name groups in regular expressions, similar to python, but using simpler syntax:

 ({name}groupstring)
 
If an additional qualifier to the group is needed, such as multiline case insensitive (?si:groupstring), this is inserted after the name brace, i.e., ({name}?si:groupstring).

The substitution syntax is:

 substitionst${name}ring
 
The following syntax can be used for back references:
 ({name}groupstring) ${name}
 
(Because of the string in braces, the $ can be disambiguated from the end-of-line symbol.)

Usage: Before a pattern with named groups is used, a NamedGroupsDecoder object is created for the pattern using new NamedGroupsDecoder(String pattern). This preprocesses the regex into a java-compliant string that can be used with the Pattern class, which is accessed using String getJavaPattern(), and an internal mapping of group names to group numbers. Groups then can be accessed via the method int getGroup(String), or directly in the Matcher, m.group(ng.getNamedGroup(String)). When substitutions are needed, the second constructor can be used: NamedGroupsDecoder(String pattern, String substitution), which also creates a substitution string with java-compliant backrefs. This is accessed using String getJavaSub() and can be directly used in the methods Matcher.replaceFirst/All(String replacement).

Author:
gregor

Constructor Summary
NamedGroupsRegex(java.lang.String pattern)
          Creates a decoder for a regex string
NamedGroupsRegex(java.lang.String pattern, java.lang.String replacement)
          Creates a decoder for regex and replacement string
 
Method Summary
 int getGroup(java.lang.String name)
           
 java.util.regex.Pattern getJavaPattern()
          Gets the complile pattern for the regex.
 java.lang.String getJavaPatternString()
          get java-compliant regex string
 java.lang.String getJavaSub()
          get java-compliant substitution / replacement string
 java.util.regex.Matcher getMatcher(java.lang.CharSequence seq)
          Creates a matcher for the regex.
static void main(java.lang.String[] args)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NamedGroupsRegex

public NamedGroupsRegex(java.lang.String pattern)
Creates a decoder for a regex string

Parameters:
pattern -

NamedGroupsRegex

public NamedGroupsRegex(java.lang.String pattern,
                        java.lang.String replacement)
Creates a decoder for regex and replacement string

Parameters:
pattern -
replacement -
Method Detail

main

public static void main(java.lang.String[] args)

getJavaPattern

public java.util.regex.Pattern getJavaPattern()
Gets the complile pattern for the regex.

Returns:

getMatcher

public java.util.regex.Matcher getMatcher(java.lang.CharSequence seq)
Creates a matcher for the regex.

Parameters:
seq -
Returns:

getJavaPatternString

public final java.lang.String getJavaPatternString()
get java-compliant regex string

Returns:

getJavaSub

public final java.lang.String getJavaSub()
get java-compliant substitution / replacement string

Returns:

getGroup

public final int getGroup(java.lang.String name)