public class PerlStyleInput
extends java.lang.Object
This class uses standard java.util.regex classes for regular expression processing.
Below is some sample code:
PerlStyleInput input = new PerlStyleInput("data/rasmus_torkel/test/misc/NameAndYear.txt"); System.out.println("Test with " + input); while (input.read()) { if (input.matchC("^\\s*([A-Za-z ]*[A-Za-z])\\s+(\\d+)\\s*$")) { int groupCount = input.groupCount(); String name = input.group(1); String year = input.group(2); System.out.println(groupCount + " groups, name is " + name + ", year is " + year); } else { System.out.println(input.messageForLine("Is not valid name and year")); } } input.close();
Modifier and Type | Field and Description |
---|---|
java.lang.String |
__
This class reads lines into this field.
|
java.lang.String |
_label
Describes the nature of this input.
|
Constructor and Description |
---|
PerlStyleInput()
This constructor takes input from standard input.
|
PerlStyleInput(java.io.BufferedReader bReader,
java.lang.String label)
Most basic constructor.
|
PerlStyleInput(java.io.File file)
File constructor.
|
PerlStyleInput(java.io.InputStream inputStream,
java.lang.String label)
Input stream constructor.
|
PerlStyleInput(java.io.Reader reader,
java.lang.String label)
Reader constructor.
|
PerlStyleInput(java.lang.String fileName)
File name constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the buffered reader.
|
java.lang.String |
group(int groupNumber)
Calls java.util.regex.Matcher.group on the last generated Matcher.
|
int |
groupCount()
Calls java.util.regex.Matcher.groupCount on the last generated Matcher
|
int |
lineNr()
Gets line number.
|
boolean |
match(java.util.regex.Pattern pattern)
Matches the pattern and keeps the generated Matcher in case the invoker
wants to retrieve groups
|
boolean |
matchC(java.lang.String regularExpression)
Obtains Pattern, either from cache or by compiling, matches and keeps
Matcher in case the invoker wants to retrieve groups.
|
boolean |
matchF(java.lang.String regularExpression)
Compiles regular expression into Pattern, matches and keeps the generated
Matcher in case the invoker wants to retrieve groups.
|
java.lang.String |
messageForLine(java.lang.String message)
Adds input label and line number to message.
|
boolean |
read()
Reads the next line.
|
java.lang.String |
toString() |
public java.lang.String __
public final java.lang.String _label
public PerlStyleInput(java.io.BufferedReader bReader, java.lang.String label)
bReader
- For reading lines of input.label
- See _label
public PerlStyleInput(java.io.Reader reader, java.lang.String label)
reader
- For input.label
- See _label
public PerlStyleInput(java.io.File file)
file
- Where input comes from.public PerlStyleInput(java.lang.String fileName)
fileName
- of the file from where input comes.public PerlStyleInput(java.io.InputStream inputStream, java.lang.String label)
inputStream
- For the inputlabel
- See _label
public PerlStyleInput()
public java.lang.String toString()
toString
in class java.lang.Object
public void close()
public boolean read()
public int lineNr()
public boolean match(java.util.regex.Pattern pattern)
pattern
- The pattern to matchpublic boolean matchF(java.lang.String regularExpression)
matchC()
, does not cache the compiled pattern.
The F in the method name stands for "Forget pattern", in other words
"Do not cache pattern".regularExpression
- For the patternpublic boolean matchC(java.lang.String regularExpression)
regularExpression
- For the patternpublic java.lang.String group(int groupNumber)
groupNumber
- public int groupCount()
groupNumber
- public java.lang.String messageForLine(java.lang.String message)
message
- Without input label and line number