Skip to content

Instantly share code, notes, and snippets.

View craigsdennis's full-sized avatar

Craig Dennis craigsdennis

View GitHub Profile
@craigsdennis
craigsdennis / Okay.java
Last active August 29, 2015 14:21
Regular Expression Example using Java
String title = "Regular Expressions in 10 different languages";
// Note we have to escape the \ as that is an escape character in Java
boolean result = title.matches("\\d+");
// To extract the pattern
Pattern pattern = Pattern.compile("(\\d+)");
Matcher matcher = pattern.match(title);
while (matcher.find()) {
System.out.printf("There are %s languages represented.",
@craigsdennis
craigsdennis / okay.py
Last active August 29, 2015 14:21
Regular Expression Example in Python
import re
title = "Regular Expressions in 10 different languages"
result = re.match(r'.*\d+.*', title)
match = re.search(r'(\d+)', title)
# This will return None match is not found
if (match):
print('There are {} languages represented.'.format(match.group(1))
@craigsdennis
craigsdennis / okay.rb
Last active August 29, 2015 14:21
Regular Expression example in Ruby
title = "Regular Expressions in 10 different languages"
# Nil if match not found
result = /.*\d+.*/.match(title)
# And extraction too
result = /(\d+)/.match(title)
if result
puts "There are %s languages represented" % result[1]
end
@craigsdennis
craigsdennis / okay.js
Last active August 29, 2015 14:21
Regular Expression example in JavaScript
var title = "Regular Expressions in 10 different languages";
// This will be true or false
var result = /.*\d+.*/.test(title);
var match = /(\d+)/.exec(title);
if (match) {
console.log("There are ", match[1], " languages represented.");
}
@craigsdennis
craigsdennis / okay.php
Last active August 29, 2015 14:21
Regular Expressions example in PHP
$title = "Regular Expressions in 10 different languages";
$pattern = "~(\\d+)~";
$success = preg_match($pattern, $title, $match);
if ($success) {
echo "There are " . $match[1] . " languages represented.";
}
@craigsdennis
craigsdennis / okay.cs
Last active August 29, 2015 14:21
Regular Expression example in C#
string title = "Regular Expressions in 10 different languages";
string pattern = @"(\d+)";
foreach (Match match in Regex.Matches(title, pattern))
Console.WriteLine("There are {0} languages represented.",
match.Groups[1].Value);
@craigsdennis
craigsdennis / Okay.groovy
Last active August 29, 2015 14:21
Regular Expression example in Groovy
title = "Regular Expressions in 10 different languages"
matcher = title =~ /(\d+)/
matcher.find()
printf("There are %s languages represented", matcher.group(1))
@craigsdennis
craigsdennis / Okay.scala
Last active August 29, 2015 14:21
Regular Expressions example in Scala
val title = "Regular Expressions in 10 different languages"
val pattern = """(\d+)""".r.unanchored
title match {
case pattern(total) => s"There are $total languages represented."
}
@craigsdennis
craigsdennis / Okay.swift
Last active June 8, 2016 10:40
Regular Expressions in Swift
let title = "Regular Expressions in 10 different languages"
// Match
let pattern = ".*\\d.*"
if let match = title.rangeOfString(pattern, options: .RegularExpressionSearch) {
println("We had a match!")
}
let re = NSRegularExpression(pattern: "(\\d+)", options: nil, error: nil)!
let matches = re.matchesInString(
var shhh = "SECRETPASSWORD";
function hexToRgb(hex) {
// No RegEx in Espruino yet...
var R, G, B;
if (hex.length == 3) {
R = hex.substring(0, 1);
R = R + R;
G = hex.substring(1, 2);
G = G + G;