Your Ad Here

Saturday, April 5, 2008

Java Regular Expression Samples

  • Check if the regex matches a string entirely
  • IF/else branch whether the regx matches a sring entirely
  • Create an object to use the same regx for many operations
  • Create an object to apply a regx repeatedly to a given string
  • Use regex object to test if (part of ) a string can be matched
  • Use regex object to test if a string can be match entirely
  • use regex object to get the part of a string matched by the regex
  • Use regex object to get the path of a string matched by a numbered group
  • Use regex object to get a list of all text matched by a numbered group
  • Iterate over all matches in a string
  • Iterate over all matches and capturing groups in a string
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
* Created by IntelliJ IDEA.
* User: Ishara Samantha
* Date: Apr 5, 2008
* Time: 8:46:45 PM
* To change this template use File | Settings | File Templates.
*/
public class JavaRegX
{
private static String subjectString;
private static String subjectString1;
private static String anotherSubjectString;

public static void main(String[] args)
{
subjectString = "ishara@hoofoo.net";
anotherSubjectString = "test@hoofoo.net";
subjectString1 = subjectString + "," + anotherSubjectString;

//Check if the regex matches a string entirely
try
{
boolean foundMatch = subjectString.matches("(?i)\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b");
System.out.println("foundMatch = " + foundMatch);
} catch (PatternSyntaxException ex)
{
ex.printStackTrace();
}

//IF/else branch whether the regx matches a sring entirely
try
{
if (subjectString.matches("(?i)\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b"))
{
System.out.println("Match");
} else
{
System.out.println("Match Faild");
}
} catch (PatternSyntaxException ex)
{
// Syntax error in the regular expression
}

try
{
Pattern regex = Pattern.compile("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher regexMatcher = regex.matcher(subjectString);

} catch (PatternSyntaxException ex)
{
// Syntax error in the regular expression
}

try
{
//Create an object to use the same regx for many operations
Pattern regex = Pattern.compile("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
//Create an object to apply a regx repeatedly to a given string
Matcher regexMatcher = regex.matcher(subjectString);
//Aply the same regex to more than one string
regexMatcher.reset(anotherSubjectString);

} catch (PatternSyntaxException ex)
{
// Syntax error in the regular expression
}

//Use regex object to test if (part of ) a string can be matched
boolean foundMatch = false;
try
{
Pattern regex = Pattern.compile("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher regexMatcher = regex.matcher(subjectString1);
foundMatch = regexMatcher.find();
System.out.println("regexMatcher = " + regexMatcher);
System.out.println("foundMatch = " + foundMatch);
} catch (PatternSyntaxException ex)

{
// Syntax error in the regular expression
}

//Use regex object to test if a string can be match entirely
try
{
Pattern regex = Pattern.compile("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher regexMatcher = regex.matcher(subjectString);
foundMatch = regexMatcher.matches();
System.out.println("Use regex object to test if a string can be match entirely");
System.out.println("regexMatcher = " + regexMatcher);
System.out.println("foundMatch = " + foundMatch);
} catch (PatternSyntaxException ex)
{
// Syntax error in the regular expression
}

//use regex object to get the part of a string matched by the regex
//Use regex object to get the path of a string matched by a numbered group
String ResultString = null;
try
{
Pattern regex = Pattern.compile("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher regexMatcher = regex.matcher(subjectString1);
if (regexMatcher.find())
{
ResultString = regexMatcher.group(0);
System.out.println("ResultString = " + ResultString);
}
} catch (PatternSyntaxException ex)
{
// Syntax error in the regular expression
}

//Use regex object to get a list of all text matched by a numbered group
List matchList = new ArrayList();
try
{
Pattern regex = Pattern.compile("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher regexMatcher = regex.matcher(subjectString1);
while (regexMatcher.find())
{
matchList.add(regexMatcher.group(0));
}
} catch (PatternSyntaxException ex)
{
// Syntax error in the regular expression
}
System.out.println("matchList.size() = " + matchList.size());

//Iterate over all matches in a string
try
{
Pattern regex = Pattern.compile("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find())
{
// matched text: regexMatcher.group()
// match start: regexMatcher.start()
// match end: regexMatcher.end()
}
} catch (PatternSyntaxException ex)
{
// Syntax error in the regular expression
}

//Iterate over all matches and capturing groups in a string
try
{
Pattern regex = Pattern.compile("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find())
{
for (int i = 1; i <= regexMatcher.groupCount(); i++)
{
// matched text: regexMatcher.group(i)
// match start: regexMatcher.start(i)
// match end: regexMatcher.end(i)
}
}
} catch (PatternSyntaxException ex)
{
// Syntax error in the regular expression
}


}
}

No comments: