regex snippets

Split a URL into protocol, domain, port and URI using regular expressions

Tagged regex, java, url  Languages java
// Split URL into protocol, domain, port and URI
Pattern pattern = Pattern.compile("(https?://)([^:^/]*)(:\\d*)?(.*)?");
Matcher matcher = pattern.matcher(url);


String protocol =;            
String domain   =;
String port     =;
String uri      =;

Ruby Regular expression examples

Tagged regular expression, ruby, example, regex  Languages ruby
# extract xxx
text = "{{xxx}}"
if text[/\{\{(.*)?\}\}/]
  xxx = $1

# extract all headers from a raw Wikipedia article
# example:
# ==Banks==
# ===Fraud===
# ===Corruption===
# \1 = back reference
headers = text.scan(/^(={1,5})(.+?)\1$/m).inject([]) do |result, m|
  header = => m[0].length, :title => m[1])
  result << header
  print ("    "*header.depth) + header.title + "\n"

Text processing with Ruby regular expressions and blocks

Tagged ruby, text-processing, regular-expressions, regex  Languages ruby

This example will find all code tags and pass the lang attribute's value and the code block's content to the block:

output = r.gsub(/<code[\.|\:]([\w\-]+)>(.*?)<\/code[\.|\:]?\1?>/m) do
       lang, text = $1, $2
       html = "<notextile>" + text +  "</notextile>"

The block's return value will replace the found tag.

How to install and use RegexKitLite

Tagged regular expression, objective-c, regexkitlite, regex  Languages objectivec

Install RegexKitLite

First download RegexKitLite.

Configure your project

Double-click the project target under Targets in the Groups & Files sidebar.

Under the Linking header add -licucore to the Other Linker Flags setting.

Setup documentation

In the Xcode menu, open Xcode->Preferences->Documentation. Then click the Add Publisher button.

Add this URL: feed://

Using RegexKitLite

This will extract the integer that is embedded in a td tag:

NSString *regex = @"<td id\"amount\">(\\d+)</td>";
NSString *body = [[[NSString alloc] initWithData:html encoding: NSASCIIStringEncoding] autorelease];
NSString *amount = [body stringByMatching:regex capture:1];

if ([amount isEqual:@""] == NO) {
    NSLog(@"Amount is %@", amount);
} else {
    NSLog(@"Amount was not found.");

Custom regex matcher for matchy

Tagged matchy, regex, ruby, match  Languages ruby


group :test do  
  gem 'jnunemaker-matchy'


class ActiveSupport::TestCase
  custom_matcher :match do |receiver, matcher, args|
    regex = args[0]
    matcher.positive_failure_message = "Expected #{receiver} to match #{regex}"
    matcher.negative_failure_message = "Expected #{receiver} not to match #{regex}"
    receiver.match(regex) != nil

Your test:

current_path.should match(/\/admin\/companies\/.+/)