Package Com.boco;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
public class Testreg {
/**
* Verify that the passed-in string is the entire matching regular expression
* @param regex Regular Expression
* @param decstr to match the string
* Returns True if @return matches; otherwise, returns false
*/
public static Boolean validate (String regex,string decstr) {
Expression object pattern.case_insensitive means case-insensitive matching is enabled
Pattern p = pattern.compile (regex,pattern.case_insensitive);
Create a Matcher object
Matcher m = P.matcher (DECSTR);
is exactly matched
Boolean flag = M.matches ();//The method attempts to match the entire input sequence to the pattern
return flag;
}
/**
* Verify that the passed-in string has substrings that match the regular expression
* @param regex Regular Expression
* @param decstr to match the string
* Returns True if @return matches; otherwise, returns false
*/
public static Boolean Validate2 (String regex,string decstr) {
Expression object pattern.case_insensitive means case-insensitive matching is enabled
Pattern p = pattern.compile (regex,pattern.case_insensitive);
Create a Matcher object
Matcher m = P.matcher (DECSTR);
is exactly matched
Boolean flag = M.find ();//The method scans the input sequence to find the next sub-sequence that matches the pattern
return flag;
}
/**
* If there are substrings in the given string that match the given regular expression, return the first substring that matches
* @param regex Regular Expression
* @param decstr to match the string
* @return Returns the first substring that matches, no mismatch. Returns null
*/
public static string Search (String regex,string decstr) {
Expression object pattern.case_insensitive means case-insensitive matching is enabled
Pattern p = pattern.compile (regex,pattern.case_insensitive);
Create a Matcher object
Matcher m = P.matcher (DECSTR);
If a match is found
Boolean flag = M.find ();//The method scans the input sequence to find the next sub-sequence that matches the pattern
String findString = "";
if (flag) {
FindString =m.group ();
String findString0 = M.group (0),//group (), group (0) returns a substring that conforms to the entire expression
String findString1 = M.group (1);//group (1) returns a substring that matches the first expression in a substring that conforms to the entire expression
System.out.println (FINDSTRING1);
}
return findString;
}
/**
* Returns all substrings in the given string that match the given regular expression
* @param regex Regular Expression
* @param decstr to match the string
* @return returns all substrings that match the given regular expression
*/
public static List Searchsubstr (String regex,string decstr) {
Pattern p = pattern.compile (regex,pattern.case_insensitive);
Matcher m = P.matcher (DECSTR);
list<string> list = new arraylist<string> ();
while (M.find ()) {
List.add (M.group ());
}
for (String str:list) {
System.out.println (str);
}
return list;
}
/**
* Replaces substrings in a given string that match a regular expression
* @param regex Regular Expression
* @param decstr to match the string
* @param replacestr Replace substrings that conform to the regular expression with the string
* @return Returns a new string after replacement
*/
public static string replace (string regex,string decstr,string replacestr) {
Pattern p = pattern.compile (regex,pattern.case_insensitive);
Matcher m = P.matcher (DECSTR);
Replace
String newstring = M.replaceall (REPLACESTR);
System.out.println (newstring);
return newstring;
}
public static void Testsplit () {
String str = "ABC5ADEFGHI7AJKLMN";
Segmentation
string[] STRs = Str.split ("(\\d) A");
for (int i=0;i<strs.length;i++) {
System.out.println (Strs[i]);
}
}
public static void Main (string[] args) {
Replace ("\\d", "dsfd;sa;ksd12a34b567c890d88e999f", "*");
System.out.println (Validate ("\\d+", "3344455AA"));
System.out.println (Validate2 ("\\d+", "3344455AA"));
System.out.println (Searchsubstr ("(\\d+) ([a-z]+)", "334455AA-2211AA--2255BB"));
Testsplit ();
}
}
The use of regular expressions in Java programs