Friday, August 21, 2015

Regex

Problem

How to find if string has alphanumeric characters?


Solution

Pattern p = Pattern.compile("[^0-9a-z]", Pattern.CASE_INSENSITIVE);
boolean b = p.matcher("My String").find();


Regex Lookup table

^
Matches the starting position within the string. In line-based tools, it matches the starting position of any line.
*
Matches the preceding element zero or more times. For example, ab*c matches "ac", "abc", "abbbc", etc. [xyz]* matches "", "x", "y", "z", "zx", "zyx", "xyzzy", and so on. (ab)* matches "", "ab", "abab", "ababab", and so on.
.
Matches any single character 
?
Matches the preceding element zero or one time. For example, ab?c matches only "ac" or "abc".
+
Matches the preceding element one or more times. For example, ab+c matches "abc", "abbc", "abbbc", and so on, but not "ac".
|
The choice (also known as alternation or set union) operator matches either the expression before or the expression after the operator. For example,abc|def matches "abc" or "def".
{m,n}
Matches the preceding element at least m and not more than n times. For example, a{3,5} matches only "aaa", "aaaa", and "aaaaa". 
[ ]
A bracket expression. Matches a single character that is contained within the brackets. For example, [abc] matches "a", "b", or "c". 

No comments:

Post a Comment