Tag: RegExp

Capturing groups using regular expressions (RegEx) in JavaScript

There are whole books about regular expressions so this post shouldn’t be intended as an exhaustive resource on the subject. It just shows how to extract a substring from a string using regular expressions in JavaScript so it must be considered just a tip not a tutorial on RegExp. Look at the following example:

    
var str = "https://www.alessandrolacava.com/?code=ALE69";
var regex = /code=(w+)&?/;
var results = regex.exec(str);
if(!results){
  alert("no match");
}
else{
  // first group
  alert(results[1]);
}

The previous code extracts the string that follows the code= part of str. That string is captured in the first group. of the RegExp, that’s why I use results[1] to display it. When you utilise groups–through the use of parenthesis ()–you can refer to them using indices, starting from 1. Indeed, at the index 0 you find the whole match. In the previous example, results[0] is equal to code=ALE69

Read more...

How to select any character across multiple lines in Java

You can do that using the following pattern in the compile static method of the java.util.regex.Pattern class. The pattern is (.|n|r)*? which means: any character (the .) or (the |) n or r. Zero or more times (the *) of the whole stuff.

Example: The following method strips the multiline comments (those between /* and */) from a string passed in and returns the resulting string:

 
import java.util.regex;

// ... other code 

// Strip multiline comments
public void deleteMultilineComments(String subject) {
  Pattern pattern = Pattern.compile("(/\\*(.|n|r)*?\\*/)");
  Matcher matcher = pattern.matcher(subject);
  return matcher.replaceAll("");
}

Note: rn works for Windows systems. n works for Unix-like systems. r works for Mac systems.

Read more...

How to force one or more metacharacters to be treated as ordinary characters in a Java Regular Expression (RegEx)

When using RegEx in Java you might face the need of treating one or more metacharacters as ordinary characters. As a reminder the metacharacters in a Java RegEx are:

([{^$|)?*+.

If you want to treat them as ordinary characters you have two options:

  1. Escape the metacharacter with a backslash,
  2. Enclose the whole string that contains metacharacters within Q and E

Q means: “quotes all characters until E”, while E ends the quotes.

Read more...