Java Split regex -


given string s, find number of words in string. problem word defined string of 1 or more english letters.

note: space or of special characters ![,?.\_'@+] act delimiter.

input format: string contain lower case english letters, upper case english letters, spaces, , these special characters: ![,?._'@+].

output format: on first line, print number of words in string. words don't need unique. then, print each word in separate line.

my code:

    scanner sc = new scanner(system.in);     string str = sc.nextline();     string regex = "( |!|[|,|?|.|_|'|@|+|]|\\\\)+";     string[] arr = str.split(regex);      system.out.println(arr.length);      for(int = 0; < arr.length; i++)         system.out.println(arr[i]); 

when submit code, works on half of test cases. not know test cases are. i'm asking murphy's law. situations regex implemented won't work?

you don't escape special characters in regex. let's start []. since don't escape them, part [|,|?|.|_|'|@|+|] treated set of characters |,?._'@+. means regex doesn't split on [ , ].

for example x..]y+[z split x, ]y , [z.

you can fix escaping characters. force escape more of them , end proper definition:

string regex = "( |!|\\[|,|\\?|\\.|_|'|@|\\+|\\])+"; 

note instead of defining alternatives, use set make regex easier read:

string regex = "[!\\[,?._'@+\\].]+"; 

in case need escape [ , ].

update:

there's problem leading special character (like in example ".hi?there[broski.]@@@@@"). need split on produces empty string in results. don't think there's way use split function without producing can mitigate removing first group before splitting using same regex:

string[] arr = str.replacefirst(regex, "").split(regex); 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -