RegExp Error

I am new to Java regex concept.

Can someone tell me the correct regex that I should use for the line below -

String exp = "ABCD_123_abc".

and the regex that I use for the above line:

regExp = "([a-zA-Z]+)_([0-9]+)_([az]+)"

But the code output below is "**No Match Found**"

 Public static void main() { String exp = "ABCD_123_abc"; String regExp = "([a-zA-Z]+)_([0-9]+)_([az]+)"; Pattern pattern = Pattern.compile(exp); Matcher matcher = pattern.matcher(regExp); if(matcher.matches()) { System.out.println("Match found"); } else { System.out.println(" NO Match found"); } } 
+7
source share
8 answers

The problem is this: you accidentally replaced using the regex pattern and expression to validate

 String exp = "ABCD_123_abc"; String regExp = "([a-zA-Z]+)_([0-9]+)_([az]+)"; 

Should use

 Pattern pattern = Pattern.compile(regExp); Matcher matcher = pattern.matcher(exp); 

The Pattern.compile (String regex) function accepts a regular expression.

EDIT

I apologize, my first decision really was something that should never, never, never have been: the names of the variables were contradictory with the meaning of their values ​​... This means pain and tears, and the angry colleagues getting into a scream. And there is no real defense of this crime ...

EDIT2 You can get the individual mapped groups using the Matcher.group (int) function:

 String matchedStringpart matcher.group(2); 

Note. I used 2 as an argument:

  • 0 means match input sequence
  • 1 means the first group ( ABC in this case)
  • ... etc.

If you only need part 123 , I would rewrite the regex for clarity:

 regExp = "[a-zA-Z]+_([0-9]+)_[az]+"; 

However, in this case, group() must be called using 1 , since now the first (and only) consistent group is the first:

 String matchedStringpart matcher.group(1); 
+12
source

You are not compiling a regex. You need

 Pattern pattern = Pattern.compile(regExp); Matcher matcher = pattern.matcher(exp); 

i.e. your code above is confusing regex and input line. However, your actual regex is correct.

+8
source

Your regex is great .

The problem arises because you replaced exp and regExp with your code. The compile function takes a regular expression as an argument, while the matcher function takes an expression that matches.

+5
source

Your (edited) regexp is great.

If you want to extract 123 , you can use matcher.group(2) . This method can only be called after matches or find . matcher.group(n) returns the nth capture group . A capture group is part of your regular expression enclosed in parentheses. matcher.group(0) returns the matched string.

Example

 if(matcher.matches()) { System.out.println(matcher.group(0)); System.out.println(matcher.group(1)); System.out.println(matcher.group(2)); System.out.println(matcher.group(3)); } 

prints

  ABCD_123_abc ABCD 123 abc 
+4
source

if(exp.matches(regExp))

It's enough. You do not need a template / matrix unless you have other needs.

+2
source

In this case, if you want to get 123, use the following code:

  System.out.println(matcher.group(2)); 

The fingerprint is displayed as follows: 123

Your regular expression is fine.

+1
source

This pattern will work — it matches any number of uppercase or lowercase letters, and then underscore, then number of digits, and then underscore, and then number of uppercase or lowercase letters. If you want to be more specific, you can use {n} rather than + to match a specific number of characters.

 public static void main(String[] args) { final String myString = "ABCD_123_abc"; final Pattern p = Pattern.compile("^[A-Za-z]++_(\\d++)_[A-Za-z]++$"); final Matcher matcher = p.matcher(myString); if (matcher.matches()) { System.out.println(matcher.group(1)); } } 
+1
source

Compile the regular expression first, and then compare it with matcher.

 Pattern pattern = Pattern.compile(regExp); Matcher matcher = pattern.matcher(exp); 
0
source

All Articles