To Test Your Pattern :https://regexone.com/
String str = "qwerty-1qwerty-2 455 f0gfg 4";
str = str.replaceAll("[^-?0-9]+", " ");
System.out.println(Arrays.asList(str.trim().split(" ")));
Output:
[-1, -2, 455, 0, 4]
Description
[^-?0-9]+
+
Between one and unlimited times, as many times as possible, giving back as needed-?
One of the characters “-?”0-9
A character in the range between “0” and “9”
First of all, let's take a look at two special symbols: '^' and '$'. These symbols indicate the start and the end of a string, respectively:
"^The"
|
matches any string that starts with "The".
|
"of despair$"
|
matches a string that ends in with "of despair".
|
"^abc$"
|
a string that starts and ends with "abc" - effectively an exact match comparison.
|
"notice"
|
a string that has the text "notice" in it.
|
You can see that if you don't use either of these two characters, you're saying that the pattern may occur anywhere inside the string -- you're not "hooking" it to any of the edges.
'*', '+', and '?'
In addition, the symbols '*', '+', and '?', denote the number of times a character or a sequence of characters may occur. What they mean is: "zero or more", "one or more", and "zero or one." Here are some examples:
"ab*"
|
matches a string that has an a followed by zero or more b's ("ac", "abc", "abbc", etc.)
|
"ab+"
|
same, but there's at least one b ("abc", "abbc", etc., but not "ac")
|
"ab?"
|
there might be a single b or not ("ac", "abc" but not "abbc").
|
"a?b+$"
|
a possible 'a' followed by one or more 'b's at the end of the string:
Matches any string ending with "ab", "abb", "abbb" etc. or "b", "bb" etc. but not "aab", "aabb" etc.
|
Braces { }
You can also use bounds, which appear inside braces and indicate ranges in the number of occurrences:
"ab{2}"
|
matches a string that has an a followed by exactly two b's ("abb")
|
"ab{2,}"
|
there are at least two b's ("abb", "abbbb", etc.)
|
"ab{3,5}"
|
from three to five b's ("abbb", "abbbb", or "abbbbb")
|
Note that you must always specify the first number of a range (i.e., "{0,2}", not "{,2}"). Also, as you might have noticed, the symbols '*', '+', and '?' have the same effect as using the bounds "{0,}", "{1,}", and "{0,1}", respectively.
Now, to quantify a sequence of characters, put them inside parentheses:
"a(bc)*"
|
matches a string that has an a followed by zero or more copies of the sequence "bc"
|
"a(bc){1,5}"
|
one through five copies of "bc."
|
'|' OR operator
There's also the '|' symbol, which works as an OR operator:
"hi|hello"
|
matches a string that has either "hi" or "hello" in it
|
"(b|cd)ef"
|
a string that has either "bef" or "cdef"
|
"(a|b)*c"
|
a string that has a sequence of alternating a's and b's ending in a c
|
('.')
A period ('.') stands for any single character:
"a.[0-9]"
|
matches a string that has an a followed by one character and a digit
|
"^.{3}$"
|
a string with exactly 3 characters
|
Bracket expressions
specify which characters are allowed in a single position of a string:
"[ab]"
|
matches a string that has either an a or a b (that's the same as "a|b")
|
"[a-d]"
|
a string that has lowercase letters 'a' through 'd' (that's equal to "a|b|c|d" and even "[abcd]")
|
"^[a-zA-Z]"
|
a string that starts with a letter
|
"[0-9]%"
|
a string that has a single digit before a percent sign
|
",[a-zA-Z0- 9]$"
|
a string that ends in a comma followed by an alphanumeric character
|
You can also list which characters you DON'T want -- just use a '^' as the first symbol in a bracket expression (i.e., "%[^a- zA-Z]%" matches a string with a character that is not a letter between two percent signs).
Backslash Sequences | |
\d | Match any character in the range 0 - 9 (equivalent of POSIX [:digit:]) |
\D | Match any character NOT in the range 0 - 9 (equivalent of POSIX [^[:digit:]]) |
\s | Match any whitespace characters (space, tab etc.). (equivalent of POSIX [:space:] EXCEPT VT is not recognized) |
\S | Match any character NOT whitespace (space, tab). (equivalent of POSIX [^[:space:]]) |
\w | Match any character in the range 0 - 9, A - Z, a - z and punctuation (equivalent of POSIX [:graph:]) |
\W | Match any character NOT the range 0 - 9, A - Z, a - z and punctuation (equivalent of POSIX [^[:graph:]]) |
Positional Abbreviations | |
\b | Word boundary. Match any character(s) at the beginning (\bxx) and/or end (xx\b) of a word, thus \bton\b will find ton but not tons, but \bton will find tons. |
\B | Not word boundary. Match any character(s) NOT at the beginning(\Bxx) and/or end (xx\B) of a word, thus \Bton\B will find wantons but not tons, but ton\B will find both wantons and tons. |
Metacharacter | Meaning |
? | The ? (question mark) matches when the preceding character occurs 0 or 1 times only, for example, colou?r will find both color (u is found 0 times) and colour (u is found 1 time). |
* | The * (asterisk or star) matches when the preceding character occurs 0 or more times, for example, tre* will find tree (e is found 2 times) and tread (e is found 1 time) and trough (e is found 0 times and thus returns a match only on the tr). |
+ | The + (plus) matches when the preceding character occurs 1 or more times, for example, tre+ will find tree (e is found 2 times) and tread (e is found 1 time) but NOT trough (0 times). |
{n} | Matches when the preceding character, or character range, occurs n times exactly, for example, to find a local phone number we could use [0-9]{3}-[0-9]{4} which would find any number of the form 123-4567. Value is enclosed in braces (curly brackets).Note: The - (dash) in this case, because it is outside the square brackets, is a literal. Louise Rains writes to say that it is invalid to commence a NXX code (the 123) with a zero (which would be permitted in the expression above). In this case the expression [1-9][0-9]{2}-[0-9]{4} would be necessary to find a valid local phone number. |
{n,m} | Matches when the preceding character occurs at least n times but not more than m times, for example, ba{2,3}b will find baab and baaab but NOT bab or baaaab. Values are enclosed in braces (curly brackets). |
{n,} | Matches when the preceding character occurs at least n times, for example, ba{2,}b will find 'baab', 'baaab' or 'baaaab' but NOT 'bab'. Values are enclosed in braces (curly brackets). |
() | The ( (open parenthesis) and ) (close parenthesis) may be used to group (or bind) parts of our search expression together. Officially this is called a subexpression (a.k.a. a submatch or group) and subexpressions may be nested to any depth. Parentheses (subexpresions) also capture the matched element into a variable that may be used as a backreference. See this example for its use in binding OR more about subexpressions (aka grouping or submatching) and their use as backreferences. |
| | The | (vertical bar or pipe) is called alternation in techspeak and means find the left hand OR right values, for example, gr(a|e)y will find 'gray' or 'grey' and has the sense that - having found the literal characters 'gr' - if the first test is not valid (a) the second will be tried (e), if the first is valid the second will not be tried. Alternation can be nested within each expression, thus gr((a|e)|i)y will find 'gray', 'grey' and 'griy'. |
---------------------------------------------------------------------------------------------------------------------------------------------
Split String based on space:
package com.pageobjects;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
public class Split {
public static void main(String[] args) {
//First way to split a string based on space
String str = "Hello I'm your String";
String[] splitStr = str.split("\\s+");
List<String> list = Arrays.asList(splitStr);
for (String s : list) {
System.out.println(s.trim());
}
final Pattern SPACE = Pattern.compile(" ");
String str1 = "Hello I'm your String";
String[] arr = SPACE.split(str1);
List<String> list1 = Arrays.asList(arr);
for (String s1 : list1) {
System.out.println(s1.trim());
}
}
}
No comments:
Post a Comment