Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Boell Fischer   on May 18 In Java Category.

  
Question Answered By: Katrina Edwards   on May 18

use Java Regex (only available on JDK 1.4)...
here is my code :

/*****************************************************************
import java.util.regex.*;

public class MyRegex {
public static void main(String[] args) {
myInput = "23AM";
myRegex = "^([0-9]{1,2})([AP]M)$" ;
// this regex mean : match only with a string  that have 1 or 2 num chars and
//followed by a/A or p/P and m/M ....

myPattern = Pattern.compile(myRegex);
myMatcher = myPattern.matcher(myInput);

if(myMatcher.find()){
System.out.println("Matched "+ myMatcher.groupCount());
for(int i=0; i <= myMatcher.groupCount(); i++) {
// print the matched, 0th index for indicates the global matching
System.out.println(i+" "+myMatcher.group(i));
}
} else
{
// input was invalid / not matched / not found
System.out.println("Unmatched");
}
}

private static String myRegex ; // string that contain regex pattern ;
private static Pattern myPattern ;
private static String myInput ;
private static Matcher myMatcher ;
}

Share: 

 

This Question has 4 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on tokenize name to 23 and PM separately Or get search suggestion and latest updates.


Tagged: