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

  • SEARCH
  • Home
  • 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 9143385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:00:41+00:00 2026-06-17T10:00:41+00:00

Text example: 1 00:00:00,000 –> 00:00:01,000 This is the first line 2 00:00:01,000 –>

  • 0

Text example:

1
00:00:00,000 --> 00:00:01,000
This is the first line

2
00:00:01,000 --> 00:00:02,000
This is the second line

3
00:00:02,000 --> 00:00:03,000
This is the last line

In JavaScript I would parse this with a regular expression certainly. I’m just wondering, is that the best way to do this in Obj C? I’m sure I could figure out a way to do this, but I’m wanting to do it an appropriate way.

I only need to know where to start and I’m happy to do the rest, but for understanding sake I’m going to end up with something like this (pseudo code):

NSDictionary
index -> [0-9]+
start -> hh:mm:ss,mmm
end -> hh:mm:ss,mmm
text -> one of the lines of text

In this case, I’d be parsing three entries into my dictionary.

  • 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-17T10:00:42+00:00Added an answer on June 17, 2026 at 10:00 am

    Some background: I wrote a small app and created a file called stuff.srt containing your examples that resides in the bundle; hence, my means of accessing it.

    This is just a quick and dirty thing, a proof-of-concept. Note that it doesn’t check results. Real applications always check their results. As you can see, the work takes place in the -applicationDidFinishLaunching: method (I’m working in Mac OS X, not iOS).

    EDIT:

    It’s been pointed out that the code as originally posted didn’t handle multiple text lines correctly. To address this, I take advantage of the fact that SRT files use CRLF as their line breaks, and search for two occurrences of this sequence. I then change all occurrences of CRLF in the text string to spaces, based on what I observed here. This doesn’t account for leading or trailing spaces in each line of the text.

    I changed the contents of the stuff.srt file to this:

    1
    00:00:00,000 --> 00:00:01,000
    This is the first line
    and it has a secondary line
    
    2
    00:00:01,000 --> 00:00:02,000
    This is the second line
    
    3
    00:00:02,000 --> 00:00:03,000
    This is the last line
    and it has a secondary line too
    

    and the code has been revised as follows (I also put everything into an @autoreleasepool directive; there might be a lot of autoreleased objects generated in the course of parsing the file!):

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"stuff" ofType:@"srt"];
    
        NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
    
        NSScanner *scanner = [NSScanner scannerWithString:string];
    
        while (![scanner isAtEnd])
        {
            @autoreleasepool
            {
                NSString *indexString;
                (void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&indexString];
    
                NSString *startString;
                (void) [scanner scanUpToString:@" --> " intoString:&startString];
    
                // My string constant doesn't begin with spaces because scanners
                // skip spaces and newlines by default.
                (void) [scanner scanString:@"-->" intoString:NULL];
    
                NSString *endString;
                (void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&endString];
    
                NSString *textString;
                // (void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&textString];
                // BEGIN EDIT
                (void) [scanner scanUpToString:@"\r\n\r\n" intoString:&textString];
                textString = [textString stringByReplacingOccurrencesOfString:@"\r\n" withString:@" "];
                // Addresses trailing space added if CRLF is on a line by itself at the end of the SRT file
                textString = [textString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
                // END EDIT
    
                NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                            indexString, @"index",
                                            startString, @"start",
                                            endString , @"end",
                                            textString , @"text",
                                            nil];
    
                NSLog(@"%@", dictionary);
            }
        }
    }
    

    The revised output looks like this:

    2013-02-09 16:10:17.727 SRTFileScan[4846:303] {
        end = "00:00:01,000";
        index = 1;
        start = "00:00:00,000";
        text = "This is the first line and it has a secondary line";
    }
    2013-02-09 16:10:17.729 SRTFileScan[4846:303] {
        end = "00:00:02,000";
        index = 2;
        start = "00:00:01,000";
        text = "This is the second line";
    }
    2013-02-09 16:10:17.730 SRTFileScan[4846:303] {
        end = "00:00:03,000";
        index = 3;
        start = "00:00:02,000";
        text = "This is the last line and it has a secondary line too";
    }
    

    One other thing I learned from what I’ve read today: The SRT file format originated in France, and the comma seen in the input is the decimal separator used there.

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

Sidebar

Related Questions

I need a regular expression that will extract sentences from text file. example text
I trying to write a regular expression that would match a particular word and
I want to turn the JavaScript in this example into jQuery. Basically I've jerry-rigged
See this example to understand http://jsbin.com/ocewu alt text http://easycaptures.com/fs/uploaded/212/8042227539.png This is code of example
I am trying to parse some text files with the command line. part of
How to get text: Text example max from: <td valign=top align=left> <a href=/server?tree=xabaf class=normal>
For Example - text = Československá obchodní banka; text string contains diacritics like Č
Can anybody help me with retrieving some elements from the following example text: sdfaasdflj
I have to develop a simple parser, to read block of text for example:
<div class=example>Some example text</div> <div class=example>Some more example text</div> How can I remove all

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.