Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8330501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:08:29+00:00 2026-06-09T02:08:29+00:00

This is question about Objective-C. I wrote the program that uses regular expression with

  • 0

This is question about Objective-C. I wrote the program that uses regular expression with getting whole HTML. I have uploaded the program to GitHub. However, exception occurs.

The purpose of this program is to get the “og:image” by regular expression match. This is the image which is displayed by writing URL in Facebook. To set this image, you write in HTML as below:

<meta property="og:image"
content="http://business.nikkeibp.co.jp/article/NBD/20120727/235043/zu1.jpg">

So I wrote the program which get whole HTML and find og:image part. The code is below:

// Web page address
NSURL *url = [NSURL URLWithString:textField.text];

// Get the web page HTML
NSString *string = 
[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

// prepare regular expression to find text
NSError *error   = nil;
NSRegularExpression *regexp =
[NSRegularExpression regularExpressionWithPattern:
 @"<meta property=\"og:image\" content=\".+\""
                                          options:0
                                            error:&error];

@try {
    // find by regular expression
    NSTextCheckingResult *match =
    [regexp firstMatchInString:string options:0 range:NSMakeRange(0, string.length)];

    // get the first result
    NSRange resultRange = [match rangeAtIndex:0];
    NSLog(@"match=%@", [string substringWithRange:resultRange]); 

    if (match) {

        // get the og:image URL from the find result
        NSRange urlRange = NSMakeRange(resultRange.location + 35, resultRange.length - 35 - 1);
        NSURL *urlOgImage = [NSURL URLWithString:[string substringWithRange:urlRange]];
        imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:urlOgImage]];
    }
}

The whole code is in GitHub as below:

https://github.com/weed/p120728_GetOgImage/blob/master/GetOgImage/ViewController.m

However, sometimes this program through exception.

  • success case:http://www.nicovideo.jp/watch/1343369790

  • failure case:http://business.nikkeibp.co.jp/article/NBD/20120727/235043/?ST=pc

Screen shots is here: https://github.com/weed/p120728_GetOgImage/blob/master/readme.md

Why exception occurs? Please teach me. Thank you for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:08:31+00:00Added an answer on June 9, 2026 at 2:08 am

    My friend kindly pointed about considering Character Encoding. The character encoding of first URL page is UTF-8, and the second one is EUC-JP.

    With the code below I could get the og:image of second URL I showed above.

    - (NSString *)encodedStringWithContentsOfURL:(NSURL *)url
    {
        // Get the web page HTML
        NSData *data = [NSData dataWithContentsOfURL:url];
    
        // response
        int enc_arr[] = {
            NSUTF8StringEncoding,           // UTF-8
            NSShiftJISStringEncoding,       // Shift_JIS
            NSJapaneseEUCStringEncoding,    // EUC-JP
            NSISO2022JPStringEncoding,      // JIS
            NSUnicodeStringEncoding,        // Unicode
            NSASCIIStringEncoding           // ASCII
        };
        NSString *data_str = nil;
        int max = sizeof(enc_arr) / sizeof(enc_arr[0]);
        for (int i=0; i<max; i++) {
            data_str = [
                   [NSString alloc]
                   initWithData : data
                   encoding : enc_arr[i]
                   ];
            if (data_str!=nil) {
                   break;
            }
        }
        return data_str;    
    }
    

    I made the check library of character encoding named NSString+Encode. The whole code is in GitHub:

    https://github.com/weed/p120728_OgImageLibrary

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question about Timers for windows services got me thinking: Say I have (and
I have seen this question about deploying to WebSphere using the WAS ant tasks.
I have a question about Objective-C today involving NSMutableArray. Coming from a .net/c# background
Simple (I hope), HTML question. Let's say I have a column group that spans
This question is about variable naming style in objective c and cocoa. I just
I've a simple question about objective-c / objective-j syntax. This is a method dataForItemsAtIndexes
I have a question about writing your own init methods in objective-c. I've read
I've a simple question about if statement in Objective - C. Is this if
I'm learning Objective-C and I have a question about NSArray. Can it contain nil?
This question might seem vague, sorry. Does anybody have experience writing RegEx with Objective-C

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.