import java.io.*;
import java.util.*;
import org.apache.oro.text.*;
import org.apache.oro.text.regex.*;
public class TestRegex
{
public TestRegex()
{
}
/**
* Note that this method contains pretty much all that I need to add to Musubi.
* I just need to be able to go through my patterns, and figure out whether they are in the email message or not.
*/
public void parseLog() throws Exception
{
String log1 = "172.26.155.241 - - [26/Feb/2001:10:56:03 -0500] \"GET /IsAlive.htm HTTP/1.0\" 200 15 ";
//String regexp = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\\s-\\s-\\s\\[([^\\]]+)\\]";
String regexp = ".htm";
PatternCompiler compiler = new Perl5Compiler();
Pattern pattern = compiler.compile(regexp);
PatternMatcher matcher = new Perl5Matcher();
if (matcher.contains(log1, pattern))
{
System.out.println("The string logl contained the pattern \"" + regexp + "\"");
MatchResult result = matcher.getMatch();
System.out.println("IP: " + result.group(1));
System.out.println("Timestamp: " + result.group(2));
}
}
public void parseHTML() throws Exception
{
String html = "";
String regexpForFontTag = "<\\s*font\\s+([^>]*)\\s*>";
String regexpForFontAttrib = "([a-z]+)\\s*=\\s*\"([^\"]+)\"";
PatternCompiler compiler = new Perl5Compiler();
Pattern patternForFontTag = compiler.compile(regexpForFontTag, Perl5Compiler.CASE_INSENSITIVE_MASK);
Pattern patternForFontAttrib = compiler.compile(regexpForFontAttrib, Perl5Compiler.CASE_INSENSITIVE_MASK);
PatternMatcher matcher = new Perl5Matcher();
if (matcher.contains(html, patternForFontTag))
{
MatchResult result = matcher.getMatch();
String attrib = result.group(1);
PatternMatcherInput input = new PatternMatcherInput(attrib);
while (matcher.contains(input, patternForFontAttrib))
{
result = matcher.getMatch();
System.out.println(result.group(1) + ": " + result.group(2));
}
}
}
public void substitutelink() throws Exception
{
String link = "";
String regexpForLink = "<\\s*a\\s+href\\s*=\\s*\"http://widgets.acme.com/interface.html#([^\"]+)\">";
PatternCompiler compiler = new Perl5Compiler();
Pattern patternForLink = compiler.compile(regexpForLink, Perl5Compiler.CASE_INSENSITIVE_MASK);
PatternMatcher matcher = new Perl5Matcher();
String result = Util.substitute(matcher,
patternForLink,
new Perl5Substitution(""),
link,
Util.SUBSTITUTE_ALL);
System.out.println(result);
}
public static void main(String[] args) throws Exception
{
TestRegex test = new TestRegex();
System.out.println("\n\nLog Parsing Example");
test.parseLog();
System.out.println("\n\nHtml Example 1");
test.parseHTML();
System.out.println("\n\nHtml Example 2");
test.substitutelink();
// note that I added this here to test reading stuff from a file
String contents = openFile("c:/temp/sky.shtml");
System.out.println(contents);
}
// Open named file; read text from file into jTextArea1; report to statusBar.
private static String openFile(String fileName)
{
try
{
File file = new File(fileName);
BufferedReader br = new BufferedReader(new FileReader(file));
String content = "";
String newLine = null;
while ( (newLine = br.readLine()) != null )
{
content += newLine + "\n";
}
br.close();
return content;
}
catch (IOException e)
{
System.err.println("Error occurred trying to read file: \"" + fileName + "\"");
}
return null;
}
}
|