You should remove anchors from your regex and then use Matcher#end()
method to get the position where it stopped the previous match like this:
String i = "f698fec0-dd89-11e8-b06b-☺";
Pattern p = Pattern.compile("[\\w$&+,:;=\\[\\]{}?@#|\\\\'<>.^*()%!/~\"` -]+");
Matcher m = p.matcher(i);
if (m.lookingAt() && i.length() > m.end()) {
System.out.println("Match <" + m.group() + "> failed at: " + m.end());
}
Output:
Match <f698fec0-dd89-11e8-b06b-> failed at: 24
PS: I have used lookingAt()
to ensure that we match the pattern starting from the beginning of the region. You can use find()
as well to get the next match anywhere or else keep the start anchor in pattern as
"^[\\w$&+,:;=\\[\\]{}?@#|\\\\'<>.^*()%!/~\"` -]+"
and use find()
to effectively make it behave like the above code with lookingAt()
.
Read difference between lookingAt()
and find()
I have refactored your regex to use \w
instead of [a-zA-Z0-9_]
and used quantifier +
(meaning match 1 or more) instead of *
(meaning match 0 or more) to avoid returning success for zero-length matches.