import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?:^|\\b)(?<number>(?:\\d+|\\d{1,3}(?:,\\d{3})+)(?:\\.\\d+)?)(?<replace> ?|&[^;]{2,7};)(?<dimensions>(?<mathjax>\\$[^\\n\\$]+\\$)|(?<unit>(?<si>(?!as)(?:[QRYZEPTGMkhdcmµnpfazyrq]|da)?(?:mol|cd|Hz|Pa|Wb|lm|lx|Bq|Gy|Sv|kat|[mgsAKNJWCVFΩSTHL]))|(?<imperial>mil|P|in|ft|yd|mi|le|ftm|nmi|oz|lbs?|t|tsp|tbsp|c|fl oz|pt|qt|gal|(?:sq|cu) ?(?:in|ft|yd|mi)|ft-lbf?|lbf?-ft|deg|Btu|psi|Ly)|(?<common>mph|kph|rad|sr)|(?<binary>(?:(?:[KMGTPEZY])i?)?B)))(?=\\b|$| |[.,:;()\\[\\]\\{\\}=/*+—–-])";
final String string = "I have a 30 $m^2$ pumpernickle\n"
+ "More precisely it's 29.87$m^2$.\n"
+ "Traveling over 4,000 km, to where once there was\n"
+ "...in the year 2016 as a new method of...\n"
+ "He crashed at 50mph\n"
+ "They collided at speeds of over 200m/s\n"
+ "The reading is 2kHz.\n"
+ "The range is 20kHz–1MHz (1,000.3 kHz).\n"
+ "Just 4M words at 30MB.";
final String subst = "$<number> $<dimensions>";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html