avatar

Pattern.compile function extracts the specified characters in the string?

Pattern.compile function extracts the specified characters in the string?

bounty icon
$20
Single winner
Asked  a year ago
Viewed  0 times

I need to extract the content in a specific range of a string. After searching for the information, the Pattern.compile function in Java is used to intercept the specified string. How do i use it properly?

  • add comment
avatar

to extract the content in a specific range of a string with Pattern.compile:

The syntax

Pattern Pattern.compile(String regex, int flag)

The two parameters in the Pattern.compile function:

  1. regex means the defined rule
  2. The flag means the parameter type to be set, mainly including the following situations:
  • Pattern.CASE_INSENSITIVE(?i) By default, the case is unclear. Matching only applies to the US-ASCII character set. Let the expression match regardless of case.
  • Pattern.COMMENTS(?x) In this mode, the space characters in the expression (space, tab, carriage return in the expression) will be ignored when matching. The comment starts with # and continues to the end of this line.
  • Pattern.UNIX_LINES(?d) In this mode, only'\n' is considered as a line stop, and matches with'.','^', and'$'.
  • Pattern.MULTILINE(?m) In this mode, the up arrow and single quotation mark respectively match the beginning and end of a line. In addition,'^' still matches the beginning of the string, and'' also matches the end of the string. By default, these two expressions only match the beginning and end of the string.
  • Pattern.DOTALL : In this mode, the expression'.' can match any character, including the end character that represents a line. By default, the expression'.' does not match the end of the line.

Examples :

//string to intercept
String splitStr = “[user:name] = select name from user”;
// reg
String pattern = "(\\[+)(\\w+)\\:(\\w+)(\\]+)";
//Pattern.compile function
Matcher matcher = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(splitStr);
while (matcher.find()) {
//get=> [user:name]
String allcon = matcher.group(0);
//start from [
String tableJoin1 = matcher.group(1);
//get table name=> user
String tableName = matcher.group(2);
//get fieldname=> name
String filed = matcher.group(3);
//end position ]
String tableJoin4 = matcher.group(4);
}

The regular expression defined in this program mainly extracts the content in the four brackets, starting with the \\ symbol, followed by the intercepted symbol, and w represents the matched character. According to this rule, the corresponding parameters can be extracted: tableName represents the extracted table name, and filed represents the extracted field name.

  • Taken
  • add comment
  • 0
Sign In
Sign In
avatar

Pattern.compile function extracts the specified characters in the string?

Pattern.compile function extracts the specified characters in the string?

bounty icon
$20
Single winner
Asked  a year ago
Viewed  0 times

I need to extract the content in a specific range of a string. After searching for the information, the Pattern.compile function in Java is used to intercept the specified string. How do i use it properly?

  • add comment
avatar

to extract the content in a specific range of a string with Pattern.compile:

The syntax

Pattern Pattern.compile(String regex, int flag)

The two parameters in the Pattern.compile function:

  1. regex means the defined rule
  2. The flag means the parameter type to be set, mainly including the following situations:
  • Pattern.CASE_INSENSITIVE(?i) By default, the case is unclear. Matching only applies to the US-ASCII character set. Let the expression match regardless of case.
  • Pattern.COMMENTS(?x) In this mode, the space characters in the expression (space, tab, carriage return in the expression) will be ignored when matching. The comment starts with # and continues to the end of this line.
  • Pattern.UNIX_LINES(?d) In this mode, only'\n' is considered as a line stop, and matches with'.','^', and'$'.
  • Pattern.MULTILINE(?m) In this mode, the up arrow and single quotation mark respectively match the beginning and end of a line. In addition,'^' still matches the beginning of the string, and'' also matches the end of the string. By default, these two expressions only match the beginning and end of the string.
  • Pattern.DOTALL : In this mode, the expression'.' can match any character, including the end character that represents a line. By default, the expression'.' does not match the end of the line.

Examples :

//string to intercept
String splitStr = “[user:name] = select name from user”;
// reg
String pattern = "(\\[+)(\\w+)\\:(\\w+)(\\]+)";
//Pattern.compile function
Matcher matcher = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(splitStr);
while (matcher.find()) {
//get=> [user:name]
String allcon = matcher.group(0);
//start from [
String tableJoin1 = matcher.group(1);
//get table name=> user
String tableName = matcher.group(2);
//get fieldname=> name
String filed = matcher.group(3);
//end position ]
String tableJoin4 = matcher.group(4);
}

The regular expression defined in this program mainly extracts the content in the four brackets, starting with the \\ symbol, followed by the intercepted symbol, and w represents the matched character. According to this rule, the corresponding parameters can be extracted: tableName represents the extracted table name, and filed represents the extracted field name.

  • Taken
  • add comment
  • 0