You can find more information on lookahead and lookbehind in the Regex Tutorial. String a = "ba ca da" Īssert a.replaceAll("(?<=c)a", "X").equals("ba cX da") // Match "a" following "c" ( more)Īssert a.replaceAll("(? String a = "ab ac ad" Īssert a.replaceAll("a(?=c)", "X").equals("ab Xc ad") // Match "a" followed by "c" ( more)Īssert a.replaceAll("a(?!c)", "X").equals("Xb ac Xd") // Match "a" not followed by "c" ( more)Īssert b.replaceAll("(?=)", "X").equals("XiXeXXpXe") Īssert b.replaceAll("(?!)", "X").equals("pXnXapXlX") Īssert "5".replaceFirst("^(?=\\d$)", "0").equals("05") // prepend 0 to single-digit strings // Lookahead can be everywhere in the pattern ( more):Īssert b.replaceAll("(?=)", "X").equals("piXappX"") (The s is a mnemonic for 'single-line' mode, which is what this is called in Perl. Dotall mode can also be enabled via the embedded flag expression (s). By default this expression does not match line terminators. This sub-pattern itself is not part of the actual match though. matches any character, including a line terminator. Lookahead assertions allow you to state that a pattern only matches if it is followed by another sub-pattern. ") // "." matches any character except newline ( more)Īssert b.replaceAll(".", "X").equals("XXXXXXXXXXX") Īssert a.matches(" \\w\\w\\w") // "\\w" is short for "" ( more)Īssert b.replaceAll("\\w", "X").equals("X XXXX XXXX") Īssert " ".matches(" \\s") // "\\s" matches whitespace ("") ( more)Īssert b.replaceAll("\\s", "X").equals("IXloveXJava") Īssert "123".matches(" \\d\\d\\d") // "\\d" is short for "" ( more)Īssert !a.matches(" \\W\\W\\W") // "\\W" inverts "\\w", same as "" ( more)Īssert b.replaceAll(" \\S", "X").equals("X XXXX XXXX") Īssert "a1".replaceAll(" \\D", "X").equals("X1") Īssert a.replaceAll("b a*", "X").equals("X X X X") // "*" means " zero or more" ( more)Īssert a.replaceAll("b (aa)*, "X").equals("X ba X baaa") // apply "*" to group "aa"Īssert a.replaceAll("b *", "X").equals("X X X X") Īssert a.matches(" (ba* *)*") // complex pattern ( more)Īssert a.replaceAll("b a+", "X").equals("b X X X") // "+" means " one or more" ( more)Īssert a.replaceAll("b (aa)+, "X").equals("b ba X baaa") // apply "+" to group "aa"Īssert a.replaceAll("b +", "X").equals("b X X X") Īssert a.matches(" b( ba+)+") // complex pattern ( more)Īssert a.replaceAll("b a?", "X").equals("X X Xa Xaa") // "?" means optional (0 or 1) ( more)Īssert a.replaceAll("b (aa)?, "X").equals("X ba X Xa") // apply "?" to group "aa"Īssert a.matches(" (ba* ?)+") // complex patternĪssert a.replaceAll("b a" means "at least twice" ( more) Instances of the Matcher class are not safe for such use.Assert a.matches("a c") // "" defines a character class( more)Īssert a.replaceAll(" ", "X").equals("XXX") Īssert a.matches("") // "-" defines a character rangeĪssert a.replaceAll(" ", "X").equals("aXX") Īssert b.replaceAll(" ", "X").equals("X XXXX XXXX") Īssert b.replaceAll(" ", "X").equals("I lXve XXvX") Īssert a.matches("") // "^" means "all other characters match" ( more)Īssert a.replaceAll("", "X").equals("XbX") Īssert "x".matches(". Instances of this class are immutable and are safe for use by multiple concurrent threads. For example, /bo/ matches 'boooo' in 'A ghost booooed' and 'b' in 'A bird warbled', but nothing in 'A goat grunted'. Characters Meaning x: Matches the preceding item 'x' 0 or more times. Is equivalent to the three statements above, though for repeated matches it is less efficient since it does not allow the compiled pattern to be reused. Quantifiers indicate numbers of characters or expressions to match. The statement boolean b = Pattern.matches("a*b", "aaaaab") Predefined Character Classes D A non-digit: 0-9 s, A whitespace character: tnx0Bfr S A non-whitespace character: s w, A word character. This method compiles an expression and matches an input sequence against it in a single invocation. compile("a*b") Ī matches method is defined by this class as a convenience for when a regular expression is used just once. All of the state involved in performing a match resides in the matcher, so many matchers can share the same pattern.Ī typical invocation sequence is thus Pattern p = Pattern. The resulting pattern can then be used to create a Matcher object that can match arbitrary character sequences against the regular expression. A compiled representation of a regular expression.Ī regular expression, specified as a string, must first be compiled into an instance of this class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |