Java Regex double backslash escaping special characters -


i have regular expression i've tested regexpal works expected in regexpal not expected in java. know it's due way in characters being escaped cannot see how around it.

my expression is:pattern.compile("((([a-za-z0-9])([a-za-z0-9 ]*)\\?)+)"). i'm trying here decide whether or not valid relative path directory (just in windows now), should match things "hello", "hello world", "hello\world", "hello world\foo\bar" etc. instead match when directory name contains question mark eg. "documents?". think because of fact in expression double backslash must precede question mark quantifier, when backslash escaped what's seen compile() \? assumes escaped question mark.

is there anyway ensure question mark not escaped? i've tried inserting parentheses around double backslash escapes closing parenthesis , causes "unclosed group error"

use 4 backslashes:

pattern.compile("((([a-za-z0-9])([a-za-z0-9 ]*)\\\\?)+)")                                                ^^^^ 
  1. you need match backslash char: \.
  2. a backslash special char regexps (used predefined classes such \d example), needs escaped backslash: \\.
  3. as java uses string literals regexps, , backslash special char string literals (used line feed char \n example), each backslash needs escaped backslash: \\\\.

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -