Opened 11 years ago

Closed 10 years ago

#6493 closed Bugs (invalid)

Problem in using regex to find nested tags

Reported by: little_bird@… Owned by: John Maddock
Milestone: To Be Determined Component: regex
Version: Boost 1.48.0 Severity: Problem
Keywords: regex Cc:

Description

I want to use regex to find all "<NPC></NPC>" like tags in a text file, and tags may recursive. So I made an example, using patterns

<(?<HtmlTag>NPC)>((?<Nested><\k<HtmlTag>>)|</\k<HtmlTag>>(?<-Nested>)|.*?)*</\k<HtmlTag>>

to search string

abc<NPC><NPC>12345</NPC>1</NPC>2<NPC>3</NPC>

Well, it works well in Expresso(a cool tool in making and testing regular expressions, can be found at http://www.ultrapico.com/). But by using boost::regex_search, it returns the whole string. It seems that the 3rd "<NPC>" matches ".*?" but "</\k<HtmlTag>>".

Attachments (1)

t.cpp (759 bytes ) - added by little_bird@… 11 years ago.
An example in using regex

Download all attachments as: .zip

Change History (2)

by little_bird@…, 11 years ago

Attachment: t.cpp added

An example in using regex

comment:1 by John Maddock, 10 years ago

Resolution: invalid
Status: newclosed

Perl and .Net regular expression are similar but not the same - Perl matches the whole of the input string as well.

Suggest you try a recursive expression: http://www.boost.org/doc/libs/1_50_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html#boost_regex.syntax.perl_syntax.recursive_expressions

Note: See TracTickets for help on using tickets.