Code Monkey home page Code Monkey logo

mruby-hs-regexp's Introduction

mruby Henry Spencer's regular expression (mruby-hs-regexp)

This mrbgem provides Henry Spencer's regular expression.

This is a light-weight regular expression library.

This module is based on Henry Spencer's book library and I add some features, such as 'IGNORECASE' and 'MULTILINE'.

I may develop another mrbgem based on Henry Spencer's "BSD library", which is compliant with POSIX.2, if requested.

Example

reg = HsRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+")
str = "You can download mruby's source code from 'https://github.com/mruby/mruby'."

match_data = reg.match(str)
puts match_data[0]  # whole url.
  # => https://github.com/mruby/mruby
puts match_data[1]  # domain
  # => https://github.com

License

BSD license.

Overview

mruby has not supported regular example yet (on 2013-02-11).
mruby's regular expression, oniguruma, is a great implementation, but I think that its size is large and too rich for embedded use.
These are the reason why I tried to implement this mrbgem, mruby-hs-regexp.

This mrbgem provides two classes: HsRegexp and HsMatchData.
They are similar to Regexp and MatchData, so you can use this module easily.

Reference

HsRegexp

class methods

new

Create new instance of HsRegexp.

reg = HsRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+")

You can use the following meta-characters:

  • *: zero or more preceding element.
  • +: one or more preceding element.
  • ?: zero or one preceding element.
  • ^: beginning of the line
  • $: end of the line
  • .: one arbitrary element
  • a|b: a or b
  • [abc]: a or b or c
  • (XX): grouping

compile

Same as new

instance methods

match

Try to match with string and return HsMatchData or nil.

reg = HsRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+")
str = "You can download mruby's source code from 'https://github.com/mruby/mruby'."
match_data = reg.match(str)

Currently, String#match is not supported.

HsMatchData

instance methods

[]

Return n-th matched string. (Same as MatchData).

reg = HsRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+")
str = "You can download mruby's source code from 'https://github.com/mruby/mruby'."
match_data = reg.match(str)
p match_data[0]
 # => "https://github.com/mruby/mruby"
p match_data[1]
 # => "https://github.com"

begin and end

Return beginning and end position of the matched data.

reg = HsRegexp.new("cd")
str = "abcde"
m = reg.match(str)
p m.begin(0)
 # => 2
p m.end(0)
 # => 4

Other methods have not been implemented yet...

mruby-hs-regexp's People

Contributors

bovi avatar kyab avatar masamitsu-murase avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.