java Regex not searching for comma(,). | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 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

4th Feb 2020, 5:09 AM
Manjit Kumar
Manjit Kumar - avatar
5 Antworten
+ 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: ".*,.*"
4th Feb 2020, 7:17 AM
Tibor Santa
Tibor Santa - avatar
+ 2
I think Pattern.qote("',") might help
4th Feb 2020, 2:36 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 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
4th Feb 2020, 7:19 AM
Tibor Santa
Tibor Santa - avatar
+ 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
4th Feb 2020, 9:22 AM
Tibor Santa
Tibor Santa - avatar
0
Tibor Santa While (m.find()){ int start = m.start(); } Returns 0
4th Feb 2020, 8:18 AM
Manjit Kumar
Manjit Kumar - avatar