View Subcategory
Check if a string matches a regular expression
Display
"ok" if "Hello" matches /[A-Z][a-z]+/
perl
print 'ok' if ('Hello' =~ /[A-Z][a-z]+/);
java
if ("Hello".matches("[A-Z][a-z]+")) {
System.out.println("ok");
}
System.out.println("ok");
}
clojure
(if (re-matches #"[A-Z][a-z]+" "Hello")
(println "ok"))
(println "ok"))
Check if a string matches with groups
Display
"two" if "one two three" matches /one (.*) three/
perl
print $1 if "one two three"=~/^one (.*) three$/
java
Pattern pattern = Pattern.compile("one (.*) three");
Matcher matcher = pattern.matcher("one two three");
if (matcher.matches()) {
System.out.println(matcher.group(1));
}
Matcher matcher = pattern.matcher("one two three");
if (matcher.matches()) {
System.out.println(matcher.group(1));
}
clojure
(if-let [groups (re-matches #"one (.*) three" "one two three")]
(println (second groups)))
(println (second groups)))
