Last active
August 13, 2022 18:07
-
-
Save hugocf/66d6cd241eff921e0e02 to your computer and use it in GitHub Desktop.
Regex to parse inline Markdown links in Objective-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)parseMarkdownLinks:(NSString *)text { | |
assert(text); | |
NSError *error = NULL; | |
NSString *pattern = @"\\[([^\\]]+)\\]\\(([^\\)\"\\s]+)(?:\\s+\"(.*)\")?\\)"; | |
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error]; | |
NSArray *matches = [regex matchesInString:text options:0 range:NSMakeRange(0, [text length])]; | |
for (NSTextCheckingResult* match in matches) { | |
NSString *matchString = [text substringWithRange:[match range]]; | |
NSString *link, *url, *title; | |
NSRange linkRange = [match rangeAtIndex:1]; | |
NSRange urlRange = [match rangeAtIndex:2]; | |
NSRange titleRange = [match rangeAtIndex:3]; | |
if (linkRange.length > 0) link = [text substringWithRange:linkRange]; | |
if (urlRange.length > 0) url = [text substringWithRange:urlRange]; | |
if (titleRange.length > 0) title = [text substringWithRange:titleRange]; | |
NSLog(@"Found: >%@<\nLink: >%@<\nURL: >%@<\nTitle: >%@<", matchString, link, url, title); | |
} | |
NSLog(@"Done!"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString *pattern = @"\\[([^\\]]+)\\]\\(([^\\)\"\\s]+)(?:\\s+\"(.*)\")?\\)"; | |
Note: escape needs to be doubled \\ when inside @"" for regex to interpret it as the escape char | |
https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSRegularExpression_Class/index.html | |
\\[ Find a literal opening square bracket: \[ | |
( (start 1st capturing group) | |
[^ Followed by any character that is not… | |
\\] … A literal closing square bracket: \] | |
] | |
+ Repeated 1 or more times | |
) (stop 1st capturing group) | |
\\] Followed by a literal closing square bracket: \] | |
\\( Followed by a literal opening parenthesis: \) | |
( (start 2nd capturing group) | |
[^ Followed by any character that is not… | |
\\) … A literal closing parenthesis: \) | |
\" … Nor, a double quote: " | |
\\s … Nor, any “space” type of character: \s | |
] | |
+ Repeated 1 or more times | |
) (stop 2nd capturing group) | |
(?: (start a non-capturing group) | |
\\s Followed by any “space” type of character: \s | |
+ Repeated 1 or more times | |
\" Followed by a double quote: " | |
( (start 3rd capturing group) | |
. Followed by any character | |
* Repeated 0 or more times | |
) (stop 3rd capturing group) | |
\" Followed by a double quote: " | |
) (stop the non-capturing group) | |
? (match the non-capturing group 0 or 1 times) | |
\\) Ending in a literal closing parenthesis: \) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LINKS | |
Markdown supports two styles for creating links: inline and reference. With both styles, you use square brackets to delimit the text you want to turn into a link. | |
Inline-style links use parentheses immediately after the link text. For example: | |
This is an[example link](http://example.com/). | |
Output: | |
<p>This is an <a href="http://example.com/"> | |
example link</a>.</p> | |
Optionally, you may include a title attribute in the parentheses: | |
This is an [example link](http://example.com/ "With a Title"). Another "quoted" word. | |
Output: | |
<p>This is an <a href="http://example.com/" title="With a Title"> | |
example link</a>.</p> | |
Reference-style links allow you to refer to your links by names, which you define elsewhere in your document: | |
This is an [example link](http://example.com/ "Title"). | |
Output: | |
I get 10 times more traffic from [Google][1] than from | |
[Yahoo][2] or [MSN][3]. | |
[1]: http://google.com/ "Google" | |
[2]: http://search.yahoo.com/ "Yahoo Search" | |
[3]: http://search.msn.com/ "MSN Search" | |
Output: | |
<p>I get 10 times more traffic from <a href="http://google.com/" | |
title="Google">Google</a> than from <a href="http://search.yahoo.com/" | |
title="Yahoo Search">Yahoo</a> or <a href="http://search.msn.com/" | |
title="MSN Search">MSN</a>.</p> | |
The title attribute is optional. Link names may contain letters, numbers and spaces, but are not case sensitive: | |
I start my morning with a cup of coffee and | |
[The New York Times][NY Times]. | |
[ny times]: http://www.nytimes.com/ | |
Output: | |
<p>I start my morning with a cup of coffee and | |
<a href="http://www.nytimes.com/">The New York Times</a>.</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2015-06-10 09:14:18.640 example[39382:4738113] Found: >[example link](http://example.com/)< | |
Link: >example link< | |
URL: >http://example.com/< | |
Title: >(null)< | |
2015-06-10 09:14:18.641 example[39382:4738113] Found: >[example link](http://example.com/ "With a Title")< | |
Link: >example link< | |
URL: >http://example.com/< | |
Title: >With a Title< | |
2015-06-10 09:14:18.641 example[39382:4738113] Found: >[example link](http://example.com/ "Title")< | |
Link: >example link< | |
URL: >http://example.com/< | |
Title: >Title< | |
2015-06-10 09:14:18.642 example[39382:4738113] Done! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment