java - Regex to match an optional '+' symbol followed by any word -


in java want use regular expression match string may or may not start plus symbol , contain word after that. +adm, adm both should match .

i tried [\\+?\\w\./]+ not working .

you'll want give literal + followed ? quantifier (it means "zero or 1 of these") followed whatever character class want use "word". may want capture group. of these things explained in the documentation.


re edit:

i tried [\\+?\\w\./]+ not working .

you don't want whole thing in [], denotes character class. create capture group, use (), not []. may want new pattern("(\\+?\\w+)"): optional literal + followed 1 or more word characters, within capture group. or without capture group: new pattern("\\+?\\w+"). (i show new pattern bit it's clear within string literal, hence escaping backslashes. java really needs literal regex notation.)


Comments