Processing 3 Escaping Regex Characters

I’m currently using the Regex .matches("") command Processing 3 to make an ipv4 address format verifier. I’ve currently got it working with the seperate sets of numbers but my problem is with the ‘.’ symbol. The ‘.’ symbol is traditionally treated as a wildcard in Regex and its the same here but for some reason I can’t escape the character by using the normal method of placing a “.” back slash in front of the character. When I try this I get the error of: “Invalid escape sequence (valid ones are \b \t \n \f \r " ’ \)”. So I was just wondering if there’s any other way to get round this that I don’t know of?

1 Like

Hello,

I am not familiar with regex but did a quick exploration of this.

Example:

void setup()
  {
  String s = "192.168.001.001";
  //String[] m = match(s, "\\.");
  String[] m = match(s, "\\.(.*?)\\.");
  
  println("Found '" + m[1] + "' inside the tag.");
  printArray(m);
  }

References:
https://processing.org/reference/match_.html
https://py.processing.org/reference/string.html

Update (I had the py version above):
https://processing.org/reference/String.html
:)

2 Likes

Oh thank you very much. It appears that you need to use two backslashes to escape characters. I’ve been stuck on this for a while. I’m very grateful for your help.

1 Like

For more on this, see the reference page for String:

Because a String is defined between double quotation marks, to include such marks within the String itself you must use the ** (backslash) character. (See the third example above.) This is known as an escape sequence . Other escape sequences include \t for the tab character and \n for new line. Because backslash is the escape character, to include a single backslash within a String, you must use two consecutive backslashes, as in: \ String / Reference / Processing.org

1 Like