How to write regular expression to match a pattern in java?
up vote
0
down vote
favorite
I've the following java string:
"Java (simple) _New=AB_U748490_JAVA47BYH"
.. and I'm using the following regular expression:
"_New=[A-Z]{2}_(w{7})_(JAVA.+)";
Problem: it always returns false. But why?
java regex spring-mvc
add a comment |
up vote
0
down vote
favorite
I've the following java string:
"Java (simple) _New=AB_U748490_JAVA47BYH"
.. and I'm using the following regular expression:
"_New=[A-Z]{2}_(w{7})_(JAVA.+)";
Problem: it always returns false. But why?
java regex spring-mvc
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I've the following java string:
"Java (simple) _New=AB_U748490_JAVA47BYH"
.. and I'm using the following regular expression:
"_New=[A-Z]{2}_(w{7})_(JAVA.+)";
Problem: it always returns false. But why?
java regex spring-mvc
I've the following java string:
"Java (simple) _New=AB_U748490_JAVA47BYH"
.. and I'm using the following regular expression:
"_New=[A-Z]{2}_(w{7})_(JAVA.+)";
Problem: it always returns false. But why?
java regex spring-mvc
java regex spring-mvc
edited Nov 19 at 20:06
Rene Knop
1,2391521
1,2391521
asked Nov 19 at 20:02
Jeena
41
41
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
The way you have written your regex will work with Matcher.find() because find searches the regex in whole input string anywhere.
If you want to make your regex to match fully, you need to modify your regex little and prepend .* in the beginning of regex something like this,
.*_New=[A-Z]{2}_(\w{7})_(JAVA.+)
Notice how in java you need to escape character to \
Try with this code and it will print Matches
public static void main(String args) {
String s = "Java (simple) _New=AB_U748490_JAVA47BYH";
Pattern p = Pattern.compile(".*_New=[A-Z]{2}_(\w{7})_(JAVA.+)");
Matcher m = p.matcher(s);
if (m.matches()) {
System.out.println("Matches");
} else {
System.out.println("Didn't match");
}
}
Or alternatively, you can use find() method on Matcher object if you don't want to change your regex.
Hope that helps.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
The way you have written your regex will work with Matcher.find() because find searches the regex in whole input string anywhere.
If you want to make your regex to match fully, you need to modify your regex little and prepend .* in the beginning of regex something like this,
.*_New=[A-Z]{2}_(\w{7})_(JAVA.+)
Notice how in java you need to escape character to \
Try with this code and it will print Matches
public static void main(String args) {
String s = "Java (simple) _New=AB_U748490_JAVA47BYH";
Pattern p = Pattern.compile(".*_New=[A-Z]{2}_(\w{7})_(JAVA.+)");
Matcher m = p.matcher(s);
if (m.matches()) {
System.out.println("Matches");
} else {
System.out.println("Didn't match");
}
}
Or alternatively, you can use find() method on Matcher object if you don't want to change your regex.
Hope that helps.
add a comment |
up vote
2
down vote
The way you have written your regex will work with Matcher.find() because find searches the regex in whole input string anywhere.
If you want to make your regex to match fully, you need to modify your regex little and prepend .* in the beginning of regex something like this,
.*_New=[A-Z]{2}_(\w{7})_(JAVA.+)
Notice how in java you need to escape character to \
Try with this code and it will print Matches
public static void main(String args) {
String s = "Java (simple) _New=AB_U748490_JAVA47BYH";
Pattern p = Pattern.compile(".*_New=[A-Z]{2}_(\w{7})_(JAVA.+)");
Matcher m = p.matcher(s);
if (m.matches()) {
System.out.println("Matches");
} else {
System.out.println("Didn't match");
}
}
Or alternatively, you can use find() method on Matcher object if you don't want to change your regex.
Hope that helps.
add a comment |
up vote
2
down vote
up vote
2
down vote
The way you have written your regex will work with Matcher.find() because find searches the regex in whole input string anywhere.
If you want to make your regex to match fully, you need to modify your regex little and prepend .* in the beginning of regex something like this,
.*_New=[A-Z]{2}_(\w{7})_(JAVA.+)
Notice how in java you need to escape character to \
Try with this code and it will print Matches
public static void main(String args) {
String s = "Java (simple) _New=AB_U748490_JAVA47BYH";
Pattern p = Pattern.compile(".*_New=[A-Z]{2}_(\w{7})_(JAVA.+)");
Matcher m = p.matcher(s);
if (m.matches()) {
System.out.println("Matches");
} else {
System.out.println("Didn't match");
}
}
Or alternatively, you can use find() method on Matcher object if you don't want to change your regex.
Hope that helps.
The way you have written your regex will work with Matcher.find() because find searches the regex in whole input string anywhere.
If you want to make your regex to match fully, you need to modify your regex little and prepend .* in the beginning of regex something like this,
.*_New=[A-Z]{2}_(\w{7})_(JAVA.+)
Notice how in java you need to escape character to \
Try with this code and it will print Matches
public static void main(String args) {
String s = "Java (simple) _New=AB_U748490_JAVA47BYH";
Pattern p = Pattern.compile(".*_New=[A-Z]{2}_(\w{7})_(JAVA.+)");
Matcher m = p.matcher(s);
if (m.matches()) {
System.out.println("Matches");
} else {
System.out.println("Didn't match");
}
}
Or alternatively, you can use find() method on Matcher object if you don't want to change your regex.
Hope that helps.
edited Nov 19 at 21:32
answered Nov 19 at 20:09
Pushpesh Kumar Rajwanshi
3,4751823
3,4751823
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381818%2fhow-to-write-regular-expression-to-match-a-pattern-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown