Giter Site home page Giter Site logo

zakdances / verbalexpressions-for-objective-c Goto Github PK

View Code? Open in Web Editor NEW

This project forked from boredzo/verbalexpressions-for-objective-c

0.0 2.0 0.0 104 KB

A more verbose/readable way to write regular expressions

Home Page: https://github.com/jehna/VerbalExpressions/wiki

License: BSD 3-Clause "New" or "Revised" License

Objective-C 100.00%

verbalexpressions-for-objective-c's Introduction

Verbal Expressions for Objective-C

Regular expression syntax and Objective-C verbosity, together at last

This is an Objective-C implementation of @jehna's VerbalExpressions project. This is an original implementation; I have copied only the method names, nothing more.

Usage

Create a PRHVerbalExpression, then send messages to it to set options and build up the search pattern. Each option or pattern message returns self, so you can stack up a chain of such messages that describe the pattern.

Each PRHVerbalExpression responds to a small suite of matching methods that you can use to apply the pattern to a string.

For example:

NSString *prefix = @"Hello";
NSString *suffix = @"Your attention please!";
NSString *entireString = [NSString stringWithFormat:@"%@, people of Earth!\n%@", prefix, suffix];
PRHVerbalExpression *expression = [[[[[[[
	[PRHVerbalExpression new]
	searchMultipleLines]
	startOfLine]
	then:prefix]
	anything]
	then:suffix]
	endOfLine];
bool match = [expression matchesString:entireString];

Within the header and this documentation, a “literal string” is a string that is not examined for regular expression metacharacters (or, if it is, it is in order to escape them). So, for example, a literal string like @"[^0-9]" will match exactly six characters (or not at all): an opening square bracket, a caret, a zero, a hyphen-minus, a nine, and a closing square bracket. It will not match a single character that is not within the range of '0'–'9'.

Differences

Character classes

For character classes, the original JavaScript implementation has a method named range, taking two arguments.

This version replaces this with four different methods:

  • characterClassWithRangeFrom:(unichar)from to:(unichar)to
  • extendCharacterClassWithRangeFrom:(unichar)from to:(unichar)to
  • characterClassWithCharacter:(unichar)ch
  • extendCharacterClassWithCharacter:(unichar)ch

So, for example, a simple expression for formatted numbers might look like:

[[[
	[PRHVerbalExpression new]
	characterClassWithRangeFrom:'0' to:'9']
	extendCharacterClassWithCharacter:',']
	extendCharacterClassWithCharacter:'.'];

(This produces the regular expression [0-9,.].)

Groups

I've added methods for creating groups:

  • group:(id)literalStringOrVerbalExpression
  • nonCapturingGroup:(id)literalStringOrVerbalExpression

Each one takes either a literal string or a PRHVerbalExpression.

Multipliers

I've added methods for specifying the minimum and maximum number of times a subexpression must appear consecutively:

  • maybe:(id)literalStringOrVerbalExpression (?)
  • zeroOrMoreOf:(id)literalStringOrVerbalExpression (*)
  • oneOrMoreOf:(id)literalStringOrVerbalExpression (+)
  • atLeast:(NSUInteger)min butNoMoreThan:(NSUInteger)max ofExpression:(id)literalStringOrVerbalExpression ({min,max})

Room for improvement

Some of this isn't implemented yet, and most of it is not yet tested. Any method in PRHVerbalExpression that simply returns self is not yet implemented.

Additionally, there's room for some of the method names to be made even more verbose to conform to typical Objective-C naming conventions.

My original vision for the project was to implement some runtime hackery so that you wouldn't need to nest method calls. I still want to give that a try, although I'm not sure how to handle the no-argument methods (like startOfLine/endOfLine).

Certain things could be made more robust. For example, if you add anything (except another sub-expression) to the expression after endOfLine, you're going to have a bad time. Ideally, most of the methods should replace and re-append the $ if there is one.

Contributions are very welcome.

verbalexpressions-for-objective-c's People

Contributors

zakdances avatar boredzo avatar josholdenburg avatar

Watchers

James Cloos 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.