Invalid string literal '^.*\s(\d+)\s.*$'. Illegal character sequence \s' in string literal [duplicate]

893    Asked by FelicaLaplaca in AWS , Asked on Apr 24, 2021

 I want to use the below regex string:  private final String regex = '^.*s(d+)s.*$'; In a Pattern: private Pattern pattern = Pattern.compile(regex); But I am getting this error: Illegal string literal: Invalid string literal '^.s(d+)s.$'. Illegal character sequence s' in string literal. What do I need to do to fix this? Top keywords - *

Answered by Gordon Guess

Escape all the characters with more character of course!

private final String regex = '^.*\s(\d+)\s.*$';
Hope this will help you solve your error.

Your Answer

Interviews

Parent Categories