com.google.code.regexp
Class Pattern

java.lang.Object
  extended by com.google.code.regexp.Pattern

public class Pattern
extends Object

A compiled representation of a regular expression. This is a wrapper for the java.util.regex.Pattern with support for named capturing groups. The named groups are specified with "(?<name>exp)", which is identical to Java 7 named groups.

Since:
0.1.9

Constructor Summary
protected Pattern(String regex, int flags)
          Constructs a named pattern with the given regular expression and flags
 
Method Summary
static Pattern compile(String regex)
          Compiles the given regular expression into a pattern
static Pattern compile(String regex, int flags)
          Compiles the given regular expression into a pattern with the given flags
 boolean equals(Object obj)
           
static Map<String,List<GroupInfo>> extractGroupInfo(String namedPattern)
          Parses info on named capture groups from a pattern
 int flags()
          Returns this pattern's match flags
 Map<String,List<GroupInfo>> groupInfo()
          Gets the names and group info (group index and string position within the named pattern) of all named capture groups
 List<String> groupNames()
          Gets the names of all capture groups
 int hashCode()
           
 int indexOf(String groupName)
          Gets the group index of a named capture group
 int indexOf(String groupName, int index)
          Gets the group index of a named capture group at the specified index.
 Matcher matcher(CharSequence input)
          Creates a matcher that will match the given input against this pattern.
 String namedPattern()
          Returns the original regular expression (including named groups)
 Pattern pattern()
          Returns the wrapped Pattern
 String replaceProperties(String replacementPattern)
          Replaces group-name properties (e.g., ${named}) in a replacement pattern with the equivalent reference that uses the corresponding group index (e.g., $2).
 String[] split(CharSequence input)
          Splits the given input sequence around matches of this pattern.
 String[] split(CharSequence input, int limit)
          Splits the given input sequence around matches of this pattern.
 String standardPattern()
          Returns the regular expression from which this pattern was compiled.
 String toString()
          Returns a string representation of this pattern
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Pattern

protected Pattern(String regex,
                  int flags)
Constructs a named pattern with the given regular expression and flags

Parameters:
regex - the expression to be compiled
flags - Match flags, a bit mask that may include:
Method Detail

compile

public static Pattern compile(String regex)
Compiles the given regular expression into a pattern

Parameters:
regex - the expression to be compiled
Returns:
the pattern

compile

public static Pattern compile(String regex,
                              int flags)
Compiles the given regular expression into a pattern with the given flags

Parameters:
regex - the expression to be compiled
flags - Match flags, a bit mask that may include:
Returns:
the pattern

indexOf

public int indexOf(String groupName)
Gets the group index of a named capture group

Parameters:
groupName - name of capture group
Returns:
group index or -1 if not found

indexOf

public int indexOf(String groupName,
                   int index)
Gets the group index of a named capture group at the specified index. If only one instance of the named group exists, use index 0.

Parameters:
groupName - name of capture group
index - the instance index of the named capture group within the pattern; e.g., index is 2 for the third instance
Returns:
group index or -1 if not found
Throws:
IndexOutOfBoundsException - if instance index is out of bounds

flags

public int flags()
Returns this pattern's match flags

Returns:
The match flags specified when this pattern was compiled

matcher

public Matcher matcher(CharSequence input)
Creates a matcher that will match the given input against this pattern.

Parameters:
input - The character sequence to be matched
Returns:
A new matcher for this pattern

pattern

public Pattern pattern()
Returns the wrapped Pattern

Returns:
the pattern

standardPattern

public String standardPattern()
Returns the regular expression from which this pattern was compiled.

Returns:
The source of this pattern

namedPattern

public String namedPattern()
Returns the original regular expression (including named groups)

Returns:
The regular expression

groupNames

public List<String> groupNames()
Gets the names of all capture groups

Returns:
the list of names

groupInfo

public Map<String,List<GroupInfo>> groupInfo()
Gets the names and group info (group index and string position within the named pattern) of all named capture groups

Returns:
a map of group names and their info

replaceProperties

public String replaceProperties(String replacementPattern)
Replaces group-name properties (e.g., ${named}) in a replacement pattern with the equivalent reference that uses the corresponding group index (e.g., $2). If the string contains literal "$", it must be escaped with slash or else this call will attempt to parse it as a group-name property. This is meant to be used to transform the parameter for:

Parameters:
replacementPattern - the input string to be evaluated
Returns:
the modified string
Throws:
PatternSyntaxException - group name was not found

split

public String[] split(CharSequence input,
                      int limit)
Splits the given input sequence around matches of this pattern.

The array returned by this method contains each substring of the input sequence that is terminated by another subsequence that matches this pattern or is terminated by the end of the input sequence. The substrings in the array are in the order in which they occur in the input. If this pattern does not match any subsequence of the input then the resulting array has just one element, namely the input sequence in string form.

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

Parameters:
input - The character sequence to be split
limit - The result threshold, as described above
Returns:
The array of strings computed by splitting the input around matches of this pattern

split

public String[] split(CharSequence input)
Splits the given input sequence around matches of this pattern.

Parameters:
input - The character sequence to be split
Returns:
The array of strings computed by splitting the input around matches of this pattern

toString

public String toString()
Returns a string representation of this pattern

Overrides:
toString in class Object
Returns:
the string

extractGroupInfo

public static Map<String,List<GroupInfo>> extractGroupInfo(String namedPattern)
Parses info on named capture groups from a pattern

Parameters:
namedPattern - regex the regular expression pattern to parse
Returns:
list of group info for all named groups

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object


Copyright © 2013. All Rights Reserved.