3

Good day,

My java code is as follow:

Pattern p = Pattern.compile("^[a-zA-Z0-9$&+,:;=\\[\\]{}?@#|\\\\'<>._^*()%!/~\"`  -]*$");
String i = "f698fec0-dd89-11e8-b06b-☺";
Matcher tagmatch = p.matcher(i);
System.out.println("tagmatch is " + tagmatch.find());

As expected, the answer will be false, because there is ☺ character inside. However, I would like to show the column number that not match. For this example, it should show column 25th having the invalid character.

May I know how can I do this?

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Or just keep the start anchor.
Note that lookingAt()/find() will always return true because the * matches zero-length strings. You'll want to compare end() to i.length() to check if it's a full match or not.
Good point, i have now suggested to use + instead of *
Your code doesn't print whether the expected pattern was matched. If there is an unexpected character at the beginning, nothing is output. Otherwise, it prints something, but I don't know if the whole thing matched.
Only if you consider a partial match success, which doesn't appear to be OP's intent. See my previous comment.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.