22. Februar 2012
Regex Tester for JavaScript
Regular Expressions in Java
Java Regex Tutorial by Lars Vogel
Match word in a sentence
var sentence = 'The internet browser Mozilla Firefox awesome.'; var match = sentence.match(/(Firefox)/)[0]; document.write(match); // Firefox
Pattern pattern = Pattern.compile("(Firefox)"); Matcher matcher = pattern.matcher("The internet browser Mozilla Firefox awesome."); matcher.find(); System.out.println(matcher.group(0)); // Firefox
Matc sentence with two words
var sentence = 'I like the Mozilla Firefox browser.'; var match = sentence.match(/.*(Mozilla).*(Firefox).*/)[0]; document.write(match); // I like the Mozilla Firefox browser.
Pattern pattern = Pattern.compile(".*(Mozilla).*(Firefox).*"); Matcher matcher = pattern.matcher("I like the Mozilla Firefox browser."); matcher.find(); System.out.println(matcher.group(0)); // I like the Mozilla Firefox browser.
Match everything after the first comma (Explanation)
var sentence = 'Match everything, after the first comma.'; var match = sentence.match(/,.*$/)[0]; document.write(match); // , after the first comma.
Pattern pattern = Pattern.compile(",.*$"); Matcher matcher = pattern.matcher("Match everything, after the first comma."); matcher.find(); System.out.println(matcher.group(0)); // , after the first comma.
Match everything after the last comma (Explanation)
var sentence = 'Match everything, after the last comma, hihi.'; var match = sentence.match(/[^,]*$/)[0]; document.write(match); // hihi.
Pattern pattern = Pattern.compile("[^,]*$"); Matcher matcher = pattern.matcher("Match everything, after the last comma, hihi."); matcher.find(); System.out.println(matcher.group(0)); // hihi
Match everything when it does not contain a certain word (Explanation)
var sentence = 'I like pizza too much.'; var match = sentence.match(/^((?!pizza).)*$/); document.write(match); // null
Pattern pattern = Pattern.compile("^((?!pizza).)*$"); Matcher matcher = pattern.matcher("I like pizza too much."); matcher.find(); System.out.println(matcher.group(0)); // java.lang.IllegalStateException: No match found
Match everything after a certain word
var url = 'www.bennyn.de'; var match = url.match(/[^www.].*/)[0]; document.write(match); // bennyn.de
Pattern pattern = Pattern.compile("[^www.].*"); Matcher matcher = pattern.matcher("www.bennyn.de"); matcher.find(); System.out.println(matcher.group(0)); // bennyn.de
Match everything until a certain character
var sentence = 'The dot is missing.'; var match = sentence.match(/.*?(?=\.)/)[0]; document.write(match); // The dot is missing
Pattern pattern = Pattern.compile(".*?(?=\\.)"); Matcher matcher = pattern.matcher("The dot is missing."); matcher.find(); System.out.println(matcher.group(0)); // The dot is missing
Match domain in an URL
var url = 'http://angelcode.de:3000/room/e0e669ac5fe7ca5'; var match = url.match(/[^http:\/\/].*?(?=:)/)[0]; document.write(match); // angelcode.de
Pattern pattern = Pattern.compile("[^http:\\/\\/].*?(?=:)"); Matcher matcher = pattern.matcher("http://angelcode.de:3000/room/e0e669ac5fe7ca5"); matcher.find(); System.out.println(matcher.group(0)); // angelcode.de
Tags:

0