I tried to find the answer to my problem in the questions history but they just come back in more than one thousand and after scanning through a few tens of matching answers
You can use the pattern (?, which means "a string-position that is not preceded by a digit; followed by exactly six digits; followed by a string-position that is not followed by a digit". (The notation
(?, known as a negative lookbehind assertion, means "not preceded by
...
". The notation (?!...)
, known as a negative lookahead assertion, means "not followed by ...
". The notation \d
means a digit. The notation {n}
means "n times", so that e.g. \d{6}
means "six digits".)
That could look like this:
final String number;
{
final Matcher m = Pattern.compile("(?
Note: a previous version of this answer suggested the use of word boundaries, \b
; but one of your comments suggests that the digits might be immediately preceded or followed by Traditional Chinese characters, which are considered word characters (and therefore wouldn't trigger a word boundary), so I've changed that.