iPhone - 使用正则表达式查找并替换字符串中多次出现的标签和内容

发布于 2024-12-05 05:02:43 字数 590 浏览 1 评论 0原文

我有一个文件(假设是一个可变字符串),其中包含多个标签,如下所示:

<Tag><minitag>40.23,12.7</minitag></Tag>

我想用这个标签替换每个标签:

<Title>some hard text</Title><Value>40.23</Value><Title>some other hard text</Title><Value>12.7</Value>

使用 [xmlString ReplaceOccurrencesOfString:@"???????" withString:@"??????"选项:NSRegularExpressionSearch 范围:NSMakeRange(0, [xmlString length])];
或者任何其他方法(如果此方法无法在几分钟内实现此目标)。

当然,该文件包含许多其他内容,数字、逗号……

我该怎么做?

I have a file (let's say a mutable String) with multiple tags like this one :

<Tag><minitag>40.23,12.7</minitag></Tag>

And I'd like to replace each one by this one :

<Title>some hard text</Title><Value>40.23</Value><Title>some other hard text</Title><Value>12.7</Value>

using [xmlString replaceOccurrencesOfString:@"???????" withString:@"???????" options:NSRegularExpressionSearch range:NSMakeRange(0, [xmlString length])];
Or any other method if this one cannot achieve this goal in a couple of minutes.

Of course, the file contains many other things, numbers, commas, ...

How may I do that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终陌 2024-12-12 05:02:43

是否可以在几分钟内重写 xml 将取决于 xml 的大小,但几分钟的计算时间已经很长了。

我认为您无法通过简单的字符串替换来完成此任务。我认为您需要解析 xml,并且在解析时,您需要分解字符串(用逗号等分隔),并同时以您想要的格式编写新的 xml 文档。

这是一个解析您拥有的字符串的快速示例。当然,如果实际情况更复杂,自定义字符串解析就会变得更复杂:

- (void) DoWork
{
    NSString *xmlSource = @"<Tag><minitag>40.23,12.7</minitag></Tag>";
    NSData *data = [xmlSource dataUsingEncoding:NSUTF8StringEncoding];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    [parser setDelegate:self];
    [parser parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName caseInsensitiveCompare:@"minitag"] == NSOrderedSame)
    {
        NSLog(@"minitag");
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"chars:%@", string);
    NSArray *components = [string componentsSeparatedByString:@","];
    for (NSString *comp in components)
    {
        NSLog(@"comp:%@", comp);

        // This outputs:
        // minitag
        // chars:40.23,12.7
        // comp:40.23
        // comp:12.7    

        //
        // At this point, you have the parts parsed, maintain an NSXmlDocument 
        // (or other xml writer)
        // that you are writing to as you're parsing this
        // Add your constant string elements and your sub data elements as you go
        //
    }
}

Whether your can rewrite the xml in a couple minutes would depend on the size of the xml but a couple minutes in compute time is a l o n g time.

I don't think you'll accomplish this with simple string replace. I think you will need to parse the xml and as it's parsing, you need to break apart the string (split on commas etc...) and in parallel be writing the new xml document in the format you want.

Here's a quick example parsing the string you had. Of course if it's more complicated in reality, the custom string parsing becomes more complicated:

- (void) DoWork
{
    NSString *xmlSource = @"<Tag><minitag>40.23,12.7</minitag></Tag>";
    NSData *data = [xmlSource dataUsingEncoding:NSUTF8StringEncoding];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    [parser setDelegate:self];
    [parser parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName caseInsensitiveCompare:@"minitag"] == NSOrderedSame)
    {
        NSLog(@"minitag");
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"chars:%@", string);
    NSArray *components = [string componentsSeparatedByString:@","];
    for (NSString *comp in components)
    {
        NSLog(@"comp:%@", comp);

        // This outputs:
        // minitag
        // chars:40.23,12.7
        // comp:40.23
        // comp:12.7    

        //
        // At this point, you have the parts parsed, maintain an NSXmlDocument 
        // (or other xml writer)
        // that you are writing to as you're parsing this
        // Add your constant string elements and your sub data elements as you go
        //
    }
}
孤蝉 2024-12-12 05:02:43

不知道正则表达式,这是我所看到的最有效的方法 - 解析 xml 以提取数字,然后创建新的数字并将数字放在那里。

dunno about regexp, the most eficient way as i can see - parse xml to extract the numbers, after it create new one and place the numbers there.

定格我的天空 2024-12-12 05:02:43

找到了:

[xmlString replaceOccurrencesOfString:@"<Tag><miniTag>([0-9.-]*){1},([0-9.-]*){1}</miniTag></Tag>" withString:@"<Title>Some hard text</Title><Value>$1</Value><Title>Some other hard text</Title><Value>$2</Value>" options:NSRegularExpressionSearch range:NSMakeRange(0, [xmlString length])];

Found it :

[xmlString replaceOccurrencesOfString:@"<Tag><miniTag>([0-9.-]*){1},([0-9.-]*){1}</miniTag></Tag>" withString:@"<Title>Some hard text</Title><Value>$1</Value><Title>Some other hard text</Title><Value>$2</Value>" options:NSRegularExpressionSearch range:NSMakeRange(0, [xmlString length])];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文