Showing posts with label regexp. Show all posts
Showing posts with label regexp. Show all posts

Tuesday, July 23, 2013

Regular Expression in F#

Regular Expression in F# is used with Active pattern, in most cases.
open System.Text.RegularExpressions
let (|Matcher|_|) pattern input =
  let matcher = Regex.Match(input, pattern)
  if matcher.Success then Some (List.tail [ for group in matcher.Groups -> group.Value ])
  else None
let matchHttpUrl target =
  match target with
    | Matcher @"(https?:\/\/\S+)" result -> Some(result.Head)
    | _ -> None
> matchHttpUrl "http://www.google.com";;
val it : string option = Some "http://www.google.com"
> matchHttpUrl "https://www.google.com";;
val it : string option = Some "https://www.google.com"
> matchHttpUrl "ftp://www.google.com";;
val it : string option = None

Tuesday, March 20, 2012

Ignore case partially in Ruby

Try phosphorescence: Ignore case partially in Ruby
$ irb
irb(main):001:0> /(?i:a)bc(?i:d)/ =~ "AbcD"
=> 0
irb(main):002:0> $~
=> #<MatchData "AbcD">
irb(main):003:0> /(?i:a)bc(?i:d)/ =~ "abcd"
=> 0
irb(main):004:0> /(?i:a)bc(?i:d)/ =~ "Abcd"
=> 0
irb(main):005:0> /(?i:a)bc(?i:d)/ =~ "abcD"
=> 0
irb(main):006:0> /(?i:a)bc(?i:d)/ =~ "ABCD"
=> nil

Tuesday, January 10, 2012

I've finished reading "Regular Expressions Cookbook"

I've finished reading Regular Expressions Cookbook.

  • Between Chapter 1 and Chapter 3, This book makes us learn or re-learn the basis of Regular Expression. It is useful both for newbie and for others.
  • Between Chapter 4 and Chapter 8, This book is totally "the recipe book". We should re-read each corresponding chapters whenever we need any solutions.
  • Japanese edition contains how to deal Japanese encodings and characters in Regular Expression domain. This is also useful for Programmer dealing Japanese language.

Thursday, December 8, 2011

Ignore case partially

It's easy to ignore case of whole word in regular expression. But How do we do ignore case partially? For example, matching with AbcD, abcD, Abcd and abcd, but not matching ABCD, aBCd and so forth.

In the Japanese edition of "Regular Expressions Cookbook", there are two approaches.

mode toggle "i"

In the page #28, surround the word to ignore between (?i) and (?-i):
/(?i)a(?-i)bc(?i)d(?-i)/

group with modifier "i"

In the page #76, use modifier "i" for the word to ignore:
/(?i:a)bc(?i:d)/

Monday, December 5, 2011

Start reading Regular Expressions Cookbook

I have started reading the Japanese edition of "Regular Expressions Cookbook".

The cover of English edition is below:

In earlier page, I encountered (?i) that I never knew until reading the Recipe 2.1 on this book.

Thursday, February 25, 2010

Start/End of String/Line regular expression in Ruby

Regular expressions about "Start of string", "Start of line", "End of line" and "End of String" are difficult and complex because of differences in each programming languages. So I try to explain Ruby's case visually.

There is a multi-line string like below:
Title

one
two, three, four


five, six, seven
eight, nine, ten



And each regular expressions (\A, ^, $, \Z, \z) match like below:


Title

one
two, three, four


five, six, seven
eight, nine, ten