WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 49 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ Here's a couple of simple examples to give an idea of how VerbalExpressions work

```objc
// Create an example of how to test for correctly formed URLs
VerbalExpressions *tester = VerEx()
.startOfLine(YES)
.then(@"http")
.maybe(@"s")
.then(@"://")
.maybe(@"www")
.anythingBut(@" ")
.endOfLine(YES);
OCVerbalExpression *expressions = [[OCVerbalExpression alloc] init];
[expressions startOfLine:YES];
[expressions then:@"http"];
[expressions maybe:@"s"];
[expressions then:@"://"];
[expressions maybe:@"www"];
[expressions anythingBut:@" "];
[expressions endOfLine:YES];

// Create an example URL
NSString *testMe = @"https://www.google.com";

// Use test() method
if (tester.test(testMe)) {
// Use test: method
if ([expressions test:testMe]) {
NSLog(@"%@", @"We have a correct URL"); // This output will fire
} else {
NSLog(@"%@", @"The URL is incorrect");
Expand All @@ -42,22 +42,58 @@ NSLog(@"%@", tester); // Ouputs the actual expression used: "^(http)(s)?(:
NSString *replaceMe = @"Replace bird with a duck";

// Create an expression that seeks for word "bird"
VerbalExpressions *verEx = VerEx().find(@"bird");
OCVerbalExpression *expressions1 = [[OCVerbalExpression alloc] init];
[expressions1 find:@"bird"];

// Execute the expression like a normal RegExp object
NSString *result = verEx.replace(replaceMe, @"duck" );
NSString *result = [expressions1 replaceWithSource:replaceMe value:@"duck"];

NSLog(@"%@", result); // Outputs "Replace duck with a duck"
```

### Shorthand for string replace:

```objc
NSString *result2 = VerEx().find(@"red").replace(@"We have a red house", @"blue");
NSString *result2 = [[[OCVerbalExpression new] find:@"red"] replaceWithSource:@"We have a red house" value:@"blue"];

NSLog(@"%@", result2); // Outputs "We have a blue house"
```

### Complex example:

```objc
// Example where you have an HTML string where you want to take out img tags that have either no extension or a
// pdf, doc, docx, xls, xlsx extension
NSString *HTML = @"<div><img src=\"asd1\"></div> <div><img src=\"asd2.pdf\"></div><div><img src=\"asd3.pdf\"></div><div><img src=\"asd4.pdf\"></div><div><img src=\"asd5.pdf\"></div><div><img src=\"asd6.pdf\"></div><div><img src=\"asd7.pdf\"></div><div><img src=\"asd8.pdf\"></div><div><img src=\"asd9.pdf\"></div><div><img src=\"asd11.pdf\"></div><div><img src=\"asd111\"></div><div><img src=\"asd1111.xls\"></div><div><img src=\"asd11111.png\"></div><div><img src=\"asd222.jpg\"></div><div><img src=\"asd343.xlsx\"></div>";

OCVerbalExpression *complexExpr = [OCVerbalExpression new];
[[[[[[[complexExpr find:@"<div><img"]
anythingBut:@">"]
anyWhiteSpace]
then:@"src"]
anyWhiteSpace]
then:@"="]
anyWhiteSpace];

[complexExpr then:[OCVerbalExpression orWithObjects:@[@"'", @"\""]]];
[complexExpr anythingBut:[OCVerbalExpression orWithObjects:@[@"'", @".", @"\""]]];
[complexExpr thenWithGroup:^{
[complexExpr thenWithGroup:^{
[complexExpr then:@"."];
[complexExpr then:[OCVerbalExpression orWithObjects:@[@"pdf", @"doc", @"docx", @"xls", @"xlsx"]]];
[complexExpr then:[OCVerbalExpression orWithObjects:@[@"'", @"\""]]];
}];
[complexExpr orWithGroup:^{
[complexExpr then:@"'"];
[complexExpr or:@"\""];
}];
}];
[[[[complexExpr anyWhiteSpace] then:@">"] anyWhiteSpace] then:@"</div>"];

NSString *result2 = [complexExpr replaceWithSource:HTML value:@""];
NSLog(@"%@", result2); // <div><img src="asd11111.png"></div><div><img src="asd222.jpg"></div>
```

## API documentation

I haven't added much documentation to this repo yet, but you can find the documentation for the original JavaScript repo on their [wiki](https://github.com/jehna/VerbalExpressions/wiki). Most of the methods have been ported as of v0.1.0 of the JavaScript repo. Just be sure to use the syntax explained above rather than the dot notation :)
Expand Down
8 changes: 8 additions & 0 deletions VerbalExpressions.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
1481B6C017B8127400EA9DA4 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 1481B6BD17B8127400EA9DA4 /* [email protected] */; };
1481B6C317B8AEDA00EA9DA4 /* VerbalExpressions.m in Sources */ = {isa = PBXBuildFile; fileRef = 1481B6C217B8AEDA00EA9DA4 /* VerbalExpressions.m */; };
1481B6C417B8AEDA00EA9DA4 /* VerbalExpressions.m in Sources */ = {isa = PBXBuildFile; fileRef = 1481B6C217B8AEDA00EA9DA4 /* VerbalExpressions.m */; };
F98A791417B950B100B0871D /* OCVerbalExpression.m in Sources */ = {isa = PBXBuildFile; fileRef = F98A791317B950B100B0871D /* OCVerbalExpression.m */; };
F98A791517B950B100B0871D /* OCVerbalExpression.m in Sources */ = {isa = PBXBuildFile; fileRef = F98A791317B950B100B0871D /* OCVerbalExpression.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -57,6 +59,8 @@
1481B6BD17B8127400EA9DA4 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
1481B6C117B8AEDA00EA9DA4 /* VerbalExpressions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = VerbalExpressions.h; path = Lib/VerbalExpressions.h; sourceTree = SOURCE_ROOT; };
1481B6C217B8AEDA00EA9DA4 /* VerbalExpressions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = VerbalExpressions.m; path = Lib/VerbalExpressions.m; sourceTree = SOURCE_ROOT; };
F98A791217B950B100B0871D /* OCVerbalExpression.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OCVerbalExpression.h; sourceTree = "<group>"; };
F98A791317B950B100B0871D /* OCVerbalExpression.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OCVerbalExpression.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -159,6 +163,8 @@
1481B6C517B8AEDF00EA9DA4 /* Lib */ = {
isa = PBXGroup;
children = (
F98A791217B950B100B0871D /* OCVerbalExpression.h */,
F98A791317B950B100B0871D /* OCVerbalExpression.m */,
1481B6C117B8AEDA00EA9DA4 /* VerbalExpressions.h */,
1481B6C217B8AEDA00EA9DA4 /* VerbalExpressions.m */,
);
Expand Down Expand Up @@ -284,6 +290,7 @@
buildActionMask = 2147483647;
files = (
1481B64517B7FE7200EA9DA4 /* main.m in Sources */,
F98A791417B950B100B0871D /* OCVerbalExpression.m in Sources */,
1481B64917B7FE7200EA9DA4 /* AppDelegate.m in Sources */,
1481B6C317B8AEDA00EA9DA4 /* VerbalExpressions.m in Sources */,
);
Expand All @@ -293,6 +300,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F98A791517B950B100B0871D /* OCVerbalExpression.m in Sources */,
1481B6C417B8AEDA00EA9DA4 /* VerbalExpressions.m in Sources */,
1481B66417B7FE7300EA9DA4 /* VerbalExpressionsTests.m in Sources */,
);
Expand Down
68 changes: 49 additions & 19 deletions VerbalExpressions/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import "AppDelegate.h"
#import "VerbalExpressions.h"
#import "OCVerbalExpression.h"

@implementation AppDelegate

Expand All @@ -17,26 +17,27 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
* Testing if we have a valid URL
*/
// Create an example of how to test for correctly formed URLs
VerbalExpressions *tester = VerEx()
.startOfLine(YES)
.then(@"http")
.maybe(@"s")
.then(@"://")
.maybe(@"www")
.anythingBut(@" ")
.endOfLine(YES);

OCVerbalExpression *expressions = [[OCVerbalExpression alloc] init];
[expressions startOfLine:YES];
[expressions then:@"http"];
[expressions maybe:@"s"];
[expressions then:@"://"];
[expressions maybe:@"www"];
[expressions anythingBut:@" "];
[expressions endOfLine:YES];
// Create an example URL
NSString *testMe = @"https://www.google.com";

// Use test() method
if (tester.test(testMe)) {
// Use test: method

if ([expressions test:testMe]) {
NSLog(@"%@", @"We have a correct URL"); // This output will fire
} else {
NSLog(@"%@", @"The URL is incorrect");
}

NSLog(@"%@", tester); // Ouputs the actual expression used: "^(http)(s)?(:\/\/)(www)?([^ ]*)$"
NSLog(@"%@", expressions); // Ouputs the actual expression used: "^(http)(s)?(:\/\/)(www)?([^ ]*)$"

/*
* Replacing strings
Expand All @@ -45,19 +46,48 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
NSString *replaceMe = @"Replace bird with a duck";

// Create an expression that seeks for word "bird"
VerbalExpressions *verEx = VerEx().find(@"bird");

OCVerbalExpression *expressions1 = [[OCVerbalExpression alloc] init];
[expressions1 find:@"bird"];

// Execute the expression like a normal RegExp object
NSString *result1 = verEx.replace(replaceMe, @"duck" );
NSString *result1 = [expressions1 replaceWithSource:replaceMe value:@"duck"]; //expression.replace(replaceMe, @"duck" );

NSLog(@"%@", result1); // Outputs "Replace duck with a duck"

/*
* Shorthand for string replace:
*/
NSString *result2 = VerEx().find(@"red").replace(@"We have a red house", @"blue");
// Example where you have an HTML string where you want to take out img tags that have either no extension or a
// pdf, doc, docx, xls, xlsx extension
NSString *HTML = @"<div><img src=\"asd1\"></div> <div><img src=\"asd2.pdf\"></div><div><img src=\"asd3.pdf\"></div><div><img src=\"asd4.pdf\"></div><div><img src=\"asd5.pdf\"></div><div><img src=\"asd6.pdf\"></div><div><img src=\"asd7.pdf\"></div><div><img src=\"asd8.pdf\"></div><div><img src=\"asd9.pdf\"></div><div><img src=\"asd11.pdf\"></div><div><img src=\"asd111\"></div><div><img src=\"asd1111.xls\"></div><div><img src=\"asd11111.png\"></div><div><img src=\"asd222.jpg\"></div><div><img src=\"asd343.xlsx\"></div>";

OCVerbalExpression *complexExpr = [OCVerbalExpression new];
[[[[[[[complexExpr find:@"<div><img"]
anythingBut:@">"]
anyWhiteSpace]
then:@"src"]
anyWhiteSpace]
then:@"="]
anyWhiteSpace];

[complexExpr then:[OCVerbalExpression orWithObjects:@[@"'", @"\""]]];
[complexExpr anythingBut:[OCVerbalExpression orWithObjects:@[@"'", @".", @"\""]]];
[complexExpr thenWithGroup:^{
[complexExpr thenWithGroup:^{
[complexExpr then:@"."];
[complexExpr then:[OCVerbalExpression orWithObjects:@[@"pdf", @"doc", @"docx", @"xls", @"xlsx"]]];
[complexExpr then:[OCVerbalExpression orWithObjects:@[@"'", @"\""]]];
}];
[complexExpr orWithGroup:^{
[complexExpr then:@"'"];
[complexExpr or:@"\""];
}];
}];
[[[[complexExpr anyWhiteSpace] then:@">"] anyWhiteSpace] then:@"</div>"];



NSString *result2 = [complexExpr replaceWithSource:HTML value:@""];

NSLog(@"%@", result2); // Outputs "We have a blue house"
NSLog(@"%@", result2);

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
Expand Down
63 changes: 63 additions & 0 deletions VerbalExpressions/OCVerbalExpression.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// OCVerbalExpression.h
// VerbalExpressions
//
// The MIT License (MIT)
//
// Copyright (c) 2013 Mathew Cruz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#import <Foundation/Foundation.h>

@interface OCVerbalExpression : NSObject

@property (nonatomic, readonly) NSRegularExpression *regularExpression;

- (OCVerbalExpression *)startOfLine:(BOOL)enable;
- (OCVerbalExpression *)endOfLine:(BOOL)enable;
- (OCVerbalExpression *)find:(NSString *)value;
- (OCVerbalExpression *)then:(NSString *)value;
- (OCVerbalExpression *)thenWithGroup:(void(^)(void))valuesToGroup;
- (OCVerbalExpression *)maybe:(NSString *)value;
- (OCVerbalExpression *)anything;
- (OCVerbalExpression *)anythingBut:(NSString *)value;
- (OCVerbalExpression *)something;
- (OCVerbalExpression *)somethingBut:(NSString *)value;
- (OCVerbalExpression *)lineBreak;
- (OCVerbalExpression *)br;
- (OCVerbalExpression *)tab;
- (OCVerbalExpression *)word;
- (OCVerbalExpression *)anyWhiteSpace;
- (OCVerbalExpression *)anyOf:(NSString *)value;
- (OCVerbalExpression *)any:(NSString *)value;
- (OCVerbalExpression *)range:(NSArray *)args;
- (OCVerbalExpression *)withAnyCase:(BOOL)enable;
- (OCVerbalExpression *)searchOneLine:(BOOL)enable;
- (OCVerbalExpression *)multiple:(NSString *)value;
- (OCVerbalExpression *)or:(NSString *)value;
- (OCVerbalExpression *)orWithGroup:(void(^)(void))valuesToGroup;

+ (NSString *)orWithObjects:(NSArray *)objects;

- (BOOL)test:(NSString *)toTest;
- (NSString *)replaceWithSource:(NSString *)source value:(NSString *)value;

- (NSString *)expressionString;

@end
Loading