Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,5 @@
/* match entire string against a pattern */
boolean isNumber = "-1234.567".matches("-?\\d+(?:\\.\\d+)?");
/* substitute part of string using a pattern */
String reduceSpaces = "a b c d e f".replaceAll(" +", " ");

View file

@ -0,0 +1,12 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
...
/* group capturing example */
Pattern pattern = Pattern.compile("(?:(https?)://)?([^/]+)/(?:([^#]+)(?:#(.+))?)?");
Matcher matcher = pattern.matcher("https://rosettacode.org/wiki/Regular_expressions#Java");
if (matcher.find()) {
String protocol = matcher.group(1);
String authority = matcher.group(2);
String path = matcher.group(3);
String fragment = matcher.group(4);
}

View file

@ -0,0 +1,2 @@
/* split a string using a pattern */
String[] strings = "abc\r\ndef\r\nghi".split("\r\n?");

View file

@ -0,0 +1,4 @@
String str = "I am a string";
if (str.matches(".*string")) { // note: matches() tests if the entire string is a match
System.out.println("ends with 'string'");
}

View file

@ -0,0 +1,6 @@
import java.util.regex.*;
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher(str);
while (m.find()) {
// use m.group() to extract matches
}

View file

@ -0,0 +1,3 @@
String orig = "I am the original string";
String result = orig.replaceAll("original", "modified");
// result is now "I am the modified string"