搜尋此網誌

顯示具有 Regular Expression 標籤的文章。 顯示所有文章
顯示具有 Regular Expression 標籤的文章。 顯示所有文章

2018年2月25日 星期日

【Java】Regular Expression Pattern


判斷正負實數 



String pattern = "[+-]?((\\d+\\.?\\d*)|(\\.\\d+))";
// Create a Pattern objectPattern r = Pattern.compile(pattern);// Now create matcher object.Matcher m = r.matcher(expression);int x = 0;while (m.find()) {
    Log.d("[FIND]", x + ":-->" + m.group(x++));}

return m.find();



判斷整數加減乘除
規則

expression.replaceAll("^(0+)", "");


expression.replaceFirst("^0+", "")

2016年12月25日 星期日

【Java】Difference between matches() and find() in Java Regex

ref:
http://stackoverflow.com/a/4450166

http://stackoverflow.com/a/18409048

matches tries to match the expression against the entire string and implicitly add a ^ at the start and $ at the end of your pattern, meaning it will not look for a substring. Hence the output of this code:
public static void main(String[] args) throws ParseException {
    Pattern p = Pattern.compile("\\d\\d\\d");
    Matcher m = p.matcher("a123b");
    System.out.println(m.find());
    System.out.println(m.matches());

    p = Pattern.compile("^\\d\\d\\d$");
    m = p.matcher("123");
    System.out.println(m.find());
    System.out.println(m.matches());
}

/* output:
true
false
true
true
*/
123 is a substring of a123b so the find() method outputs true. matches() only 'sees' a123bwhich is not the same as 123 and thus outputs false.

--------------------------------------------------------------------------------------------------------------------------

matches() will only return true if the full string is matched. find() will try to find the next occurrence within the substring that matches the regex. Note the emphasis on "the next". That means, the result of calling find() multiple times might not be the same. In addition, by using find() you can call start() to return the position the substring was matched.
final Matcher subMatcher = Pattern.compile("\\d+").matcher("skrf35kesruytfkwu4ty7sdfs");
System.out.println("Found: " + subMatcher.matches());
System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
System.out.println("Found: " + subMatcher.find());
System.out.println("Found: " + subMatcher.find());
System.out.println("Matched: " + subMatcher.matches());

System.out.println("-----------");
final Matcher fullMatcher = Pattern.compile("^\\w+$").matcher("skrf35kesruytfkwu4ty7sdfs");
System.out.println("Found: " + fullMatcher.find() + " - position " + fullMatcher.start());
System.out.println("Found: " + fullMatcher.find());
System.out.println("Found: " + fullMatcher.find());
System.out.println("Matched: " + fullMatcher.matches());
System.out.println("Matched: " + fullMatcher.matches());
System.out.println("Matched: " + fullMatcher.matches());
System.out.println("Matched: " + fullMatcher.matches());
Will output:
Found: false
Found: true - position 4
Found: true - position 17
Found: true - position 20
Found: false
Found: false
Matched: false
-----------
Found: true - position 0
Found: false
Found: false
Matched: true
Matched: true
Matched: true
Matched: true
So, be careful when calling find() multiple times if the Matcher object was not reset, even when the regex is surrounded with ^ and $ to match the full string.

【Java】 Regular Expression / 正規表示法 的學習筆記 [精華]

ref:
https://www.javaworld.com.tw/jute/post/view?bid=20&id=130126

REX checker:
https://regex101.com/

http://regexr.com/