regular expression snippets

Ruby Regular expression examples

Tagged ruby, regular expression, regex, example  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"

How to install and use RegexKitLite

Tagged regexkitlite, regex, regular expression, objective-c  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.");