Java Regex Meta Character (.) and Ordinary Dot

Java RegEx meta character (.) and ordinary dot?

If you want the dot or other characters with a special meaning in regexes to be a normal character, you have to escape it with a backslash. Since regexes in Java are normal Java strings, you need to escape the backslash itself, so you need two backslashes e.g. \\.

Java - Is it possible to store metacharacters like dot in a character variable?

Yes, it is possible.

char c = '.'; 

Escaping meta-characters is only when you're dealing with Strings, not chars.

regular expression to match one or two dots

. matches any character so needs escaping i.e. \., or \\. within a Java string (because \ itself has special meaning within Java strings.)

You can then use \.\. or \.{2} to match exactly 2 dots.

Java regular expression value.split(\\.), the back slash dot divides by character?

My guess is that you are missing that backslash ('\') characters are escape characters in Java String literals. So when you want to use a '\' escape in a regex written as a Java String you need to escape it; e.g.

Pattern.compile("\.");   // Java syntax error

// A regex that matches a (any) character
Pattern.compile(".");

// A regex that matches a literal '.' character
Pattern.compile("\\.");

// A regex that matches a literal '\' followed by one character
Pattern.compile("\\\\.");

The String.split(String separatorRegex) method splits a String into substrings separated by substrings matching the regex. So str.split("\\.") will split str into substrings separated by a single literal '.' character.

Regex to extract hashtags with two dot-separated parts

You may use

String regex = "#[^.#]*[^.#\\s][^#.]*\\.\\w+";

See the regex demo and its graph:

Sample Image

Details

  • # - a # symbol
  • [^.#]* - zero or more chars other than . and #
  • [^.#\\s] - any char but ., # and whitespace
  • [^#.]* - - zero or more chars other than . and #
  • \. - a dot
  • \w+ - 1+ word chars (letters, digits or _).

Java demo:

String s = "# #.id\nendpoint/?userId=#someuser.id\nHi #someuser.name, how are you?";
String regex = "#[^.#]*[^.#\\s][^#.]*\\.\\w+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
System.out.println(matcher.group(0));
}

Output:

#someuser.id
#someuser.name

Java regex, how to split on dot, whitespace, and keep quoted words together

This regex will get you most of the way but you will still need to remove the quotes in a second step:

(\".*\"|[^\\s\\.]+)

IE:

Matcher m = Pattern.compile("(\".*\"|[^\\s\\.]+)")
.matcher("This \"majestic world\" is truly.awesome");
while (m.find()){
System.out.println(m.group(1).replaceAll("\"", ""));
}


Related Topics



Leave a reply



Submit