[Java]
Package com. metarnet. Execution. util;
Import java. io. UnsupportedEncodingException;
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
/**
* String Regular Expression matching
* Qingfan
*/
Public class StringUtil {
/**
* Function name: strToInt for usage: Convert string to integer parameter: s: string type return value: integer
**/
Public static int strToInt (String s ){
Int I = 0;
Try {
I = Integer. parseInt (s );
} Catch (NumberFormatException illl ){
I = 0;
}
Return I;
}
/**
* Function name: strToInt for usage: Convert string to integer parameter: s: string parameter: itt: integer return value: If the return value is '0', return
* Itt;
**/
Public static int strToInt (String s, int itt ){
Int I = strToInt (s );
If (I = 0)
I = itt;
Return I;
}
Public static String encode (String result, String code ){
String res = null;
Try {
Res = new String (result. getBytes (), code );
} Catch (UnsupportedEncodingException ex ){
System. out. println ("charset:" + ex. getMessage ());
Res = result;
}
Return res;
}
/**
* If the regular expression matches, the number of successful matches is returned.
*
* @ Param str
* Original string
* @ Param regEx
* Regular Expression
* @ Return returns the number of successful matches.
*/
Public static int findReturnCount (String str, String regEx ){
Int count = 0;
Pattern p = Pattern. compile (regEx );
Matcher matcher = p. matcher (str );
While (matcher. find ()){
Count ++;
}
Return count;
}
/**
* If the regular expression matches successfully once, true is returned.
*
* @ Param str
* Original string
* @ Param regEx
* Regular Expression
* @ Return returns true if the match is successful once.
*/
Public static boolean findReturn (String str, String regEx ){
Boolean flag = false;
String [] regExs = regEx. split ("\\| ");
For (String regExStr: regExs ){
Pattern p = Pattern. compile (regExStr );
Matcher matcher = p. matcher (str );
If (matcher. find ()){
Flag = true;
Return flag;
}
}
Return false;
}
/**
* Regular Expressions split strings
*
* @ Param str
* Original string
* @ Param regEx
* Regular Expression
* @ Param child
* Sub-Match: 0 matches the entire expression, and 1 matches the expression in the first bracket...
* @ Return returns the split string array.
*/
Public static String [] splitStr (String str, String regEx, int child ){
Pattern p = Pattern. compile (regEx );
String formatStr = "";
Matcher matcher = p. matcher (str );
While (matcher. find ()){
FormatStr + = matcher. group (child) + "'";
}
If (formatStr. equals ("")){
Return str. split ("\\'");
}
Return formatStr. split ("\\'");
}
/**
* Regular Expressions split strings
*
* @ Param str
* Original string
* @ Param regEx
* Regular Expression
* @ Param child
* Sub-Match: 0 matches the entire expression, and 1 matches the expression in the first bracket...
* @ Param column
* Length of the split String Array
* @ Return returns the string array of rows whose length is equal to column after the split String Array
*/
Public static String [] splitStr (String str, String regEx, int column,
Int child ){
Pattern p = Pattern. compile (regEx );
String formatStr = "";
Matcher matcher = p. matcher (str );
While (matcher. find ()){
FormatStr + = matcher. group (child) + "'";
}
If (formatStr. equals ("")){
Return str. split ("\\'");
}
Return formatStr. split ("\\'");
}
}
For example, judge whether a string ends with an END (special characters can exist at the END, and letters, numbers, and Chinese characters cannot exist)
String returnString = "asd fsadf" +
"Asdfasdfasd fasdfa" +
"Sdf asdsdc" +
"Asdfa sdfa" +
"Dfasd fcsd" +
"Casd ascsdfEND ";
String parse = "([\ s \ S] *?) END \ W * $ ";
System. out. println (StringUtil. findReturn (returnString, parse ));
Determines whether a string starts and ends with END (special characters and numbers and Chinese characters are not allowed at the beginning and END)
Public static void main (String [] args ){
String returnString = "<ENDasd fsadf" +
"Asdfasdfasd fasdfa" +
"Sdf asdsdc" +
"Asdfa sdfa" +
"Dfasd fcsd" +
"Casd ascsdfEND> ";
String parse = "^ \ W * END ([\ s \ S] *?) END \ W * $ ";
System. out. println (StringUtil. findReturn (returnString, parse ));
}
Determines whether a string starts and ends with END (no character can exist at the beginning, special characters can exist at the END, and no letters, numbers, or Chinese characters can exist)
Public static void main (String [] args ){
String returnString = "ENDasd fsadf" +
"Asdfasdfasd fasdfa" +
"Sdf asdsdc" +
"Asdfa sdfa" +
"Dfasd fcsd" +
"Casd ascsdfEND ";
String parse = "^ END ([\ s \ S] *?) END \ W * $ ";
System. out. println (StringUtil. findReturn (returnString, parse ));
}
Determines whether a string starts and ends with END (no character can exist at the beginning or END)
Public static void main (String [] args ){
String returnString = "ENDasd fsadf" +
"Asdfasdfasd fasdfa" +
"Sdf asdsdc" +
"Asdfa sdfa" +
"Dfasd fcsd" +
"Casd ascsdfEND ";
String parse = "^ END ([\ s \ S] *?) END $ ";
System. out. println (StringUtil. findReturn (returnString, parse ));
}
Package com. metarnet. Execution. util;
Import java. io. UnsupportedEncodingException;
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
/**
* String Regular Expression matching
* Qingfan
*/
Public class StringUtil {
/**
* Function name: strToInt for usage: Convert string to integer parameter: s: string type return value: integer
**/
Public static int strToInt (String s ){
Int I = 0;
Try {
I = Integer. parseInt (s );
} Catch (NumberFormatException illl ){
I = 0;
}
Return I;
}
/**
* Function name: strToInt for usage: Convert string to integer parameter: s: string parameter: itt: integer return value: If the return value is '0', return
* Itt;
**/
Public static int strToInt (String s, int itt ){
Int I = strToInt (s );
If (I = 0)
I = itt;
Return I;
}
Public static String encode (String result, String code ){
String res = null;
Try {
Res = new String (result. getBytes (), code );
} Catch (UnsupportedEncodingException ex ){
System. out. println ("charset:" + ex. getMessage ());
Res = result;
}
Return res;
}
/**
* If the regular expression matches, the number of successful matches is returned.
*
* @ Param str
* Original string
* @ Param regEx
* Regular Expression
* @ Return returns the number of successful matches.
*/
Public static int findReturnCount (String str, String regEx ){
Int count = 0;
Pattern p = Pattern. compile (regEx );
Matcher matcher = p. matcher (str );
While (matcher. find ()){
Count ++;
}
Return count;
}
/**
* If the regular expression matches successfully once, true is returned.
*
* @ Param str
* Original string
* @ Param regEx
* Regular Expression
* @ Return returns true if the match is successful once.
*/
Public static boolean findReturn (String str, String regEx ){
Boolean flag = false;
String [] regExs = regEx. split ("\\| ");
For (String regExStr: regExs ){
Pattern p = Pattern. compile (regExStr );
Matcher matcher = p. matcher (str );
If (matcher. find ()){
Flag = true;
Return flag;
}
}
Return false;
}
/**
* Regular Expressions split strings
*
* @ Param str
* Original string
* @ Param regEx
* Regular Expression
* @ Param child
* Sub-Match: 0 matches the entire expression, and 1 matches the expression in the first bracket...
* @ Return returns the split string array.
*/
Public static String [] splitStr (String str, String regEx, int child ){
Pattern p = Pattern. compile (regEx );
String formatStr = "";
Matcher matcher = p. matcher (str );
While (matcher. find ()){
FormatStr + = matcher. group (child) + "'";
}
If (formatStr. equals ("")){
Return str. split ("\\'");
}
Return formatStr. split ("\\'");
}
/**
* Regular Expressions split strings
*
* @ Param str
* Original string
* @ Param regEx
* Regular Expression
* @ Param child
* Sub-Match: 0 matches the entire expression, and 1 matches the expression in the first bracket...
* @ Param column
* Length of the split String Array
* @ Return returns the string array of rows whose length is equal to column after the split String Array
*/
Public static String [] splitStr (String str, String regEx, int column,
Int child ){
Pattern p = Pattern. compile (regEx );
String formatStr = "";
Matcher matcher = p. matcher (str );
While (matcher. find ()){
FormatStr + = matcher. group (child) + "'";
}
If (formatStr. equals ("")){
Return str. split ("\\'");
}
Return formatStr. split ("\\'");
}
}
For example, judge whether a string ends with an END (special characters can exist at the END, and letters, numbers, and Chinese characters cannot exist)
String returnString = "asd fsadf" +
"Asdfasdfasd fasdfa" +
"Sdf asdsdc" +
"Asdfa sdfa" +
"Dfasd fcsd" +
"Casd ascsdfEND ";
String parse = "([\ s \ S] *?) END \ W * $ ";
System. out. println (StringUtil. findReturn (returnString, parse ));
Determines whether a string starts and ends with END (special characters and numbers and Chinese characters are not allowed at the beginning and END)
Public static void main (String [] args ){
String returnString = "<ENDasd fsadf" +
"Asdfasdfasd fasdfa" +
"Sdf asdsdc" +
"Asdfa sdfa" +
"Dfasd fcsd" +
"Casd ascsdfEND> ";
String parse = "^ \ W * END ([\ s \ S] *?) END \ W * $ ";
System. out. println (StringUtil. findReturn (returnString, parse ));
}
Determines whether a string starts and ends with END (no character can exist at the beginning, special characters can exist at the END, and no letters, numbers, or Chinese characters can exist)
Public static void main (String [] args ){
String returnString = "ENDasd fsadf" +
"Asdfasdfasd fasdfa" +
"Sdf asdsdc" +
"Asdfa sdfa" +
"Dfasd fcsd" +
"Casd ascsdfEND ";
String parse = "^ END ([\ s \ S] *?) END \ W * $ ";
System. out. println (StringUtil. findReturn (returnString, parse ));
}
Determines whether a string starts and ends with END (no character can exist at the beginning or END)
Public static void main (String [] args ){
String returnString = "ENDasd fsadf" +
"Asdfasdfasd fasdfa" +
"Sdf asdsdc" +
"Asdfa sdfa" +
"Dfasd fcsd" +
"Casd ascsdfEND ";
String parse = "^ END ([\ s \ S] *?) END $ ";
System. out. println (StringUtil. findReturn (returnString, parse ));
}