Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does regular expression \\s*,\\s* do?

I am wondering what this line of code does to a url that is contained in a String called surl?

String[] stokens = surl.split("\\s*,\\s*"); 

Lets pretend this is the surl = "http://myipaddress:8080/Map/MapServer.html" What will stokens be?

like image 778
spartikus Avatar asked Dec 06 '12 19:12

spartikus


People also ask

What does \\ s mean in regular expression?

The regular expression \s is a predefined character class. It indicates a single whitespace character. Let's review the set of whitespace characters: [ \t\n\x0B\f\r] The plus sign + is a greedy quantifier, which means one or more times.

What does \\ s +$ mean in Java?

\\s+ - matches sequence of one or more whitespace characters.

What is the use of \\ s?

\\s is a regular expression in Java used for white space characters. Regular expressions are the sequence of characters used to develop a pattern for data. We use patterns sometimes when we search data in the text, and \\s is used in those patterns when space is required.

What does Replaceall \\ s+ do?

\\s+ --> replaces 1 or more spaces. \\\\s+ --> replaces the literal \ followed by s one or more times.


2 Answers

That regex "\\s*,\\s*" means:

  • \s* any number of whitespace characters
  • a comma
  • \s* any number of whitespace characters

which will split on commas and consume any spaces either side

like image 150
Bohemian Avatar answered Oct 14 '22 11:10

Bohemian


  • \s stands for "whitespace character".
  • It includes [ \t\n\x0B\f\r]. That is: \s matches a space( ) or a tab(\t) or a line(\n) break or a vertical tab(\x0B sometimes referred as \v) or a form feed(\f) or a carriage return(\r) .

\\s*,\\s* 

It says zero or more occurrence of whitespace characters, followed by a comma and then followed by zero or more occurrence of whitespace characters.

These are called short hand expressions.

You can find similar regex in this site: http://www.regular-expressions.info/shorthand.html

like image 31
SRIDHARAN Avatar answered Oct 14 '22 10:10

SRIDHARAN