+ 1
java Regex not searching for comma(,).
/* text :- this is Stored in a String variable foundMatch { "firstName": "Sonoo", "lastName": "Jaiswal", "age": 27, "address" : { "streetAddress": "Plot-6, Mohan Nagar", "city": "Ghaziabad", "state": "UP", "postalCode": "201007" } } code :-- Pattern pa = Pattern.compile(","); Matcher ma = p.matcher(foundMatch); System.out.println(ma.matches()); // output :- false
5 Answers
+ 2
The regular expression "," will only match if the string is a single comma character and nothing else.
You can use Matcher.find() instead of Matcher.matches() if you are looking for the next occurrence of the pattern.
Even more simple would be to use String.contains(",") that doesn't even require a regex. Or you can modify your pattern to match anything before and after a comma, like: ".*,.*"
+ 2
I think Pattern.qote("',") might help
+ 1
Also I recommend to put your code samples in the code playground and attach to your question, that way it is much easier to analyse and test.
https://www.sololearn.com/post/75089/?ref=app
+ 1
It is working for me :)
Be aware the Matcher object is like an iterator. It starts scanning the string, and stops at the first occurrence of a hit. So when it is exhausted it won't return anything.
Here is a short demo of all methods I explained:
https://code.sololearn.com/cijwI7FMo0XJ/#java
0
Tibor Santa
While (m.find()){
int start = m.start();
}
Returns 0