Here is the task:
Output to the console all the tags that correspond to the specified tag. Each tag on a new line, the order must correspond to the sequence in the file. The number of spaces, \ n, \ r do not affect the result. The file does not contain a CDATA tag, there is a separate closing tag for all opening tags. There are no single tags. The tag may contain nested tags.
Here are the tag templates from the job:
<tag>text1</tag> <tag text2>text1</tag> <tag text2>text1</tag>
text1, text2 may be empty
Enter this:
<span>string1 <span>string2</span> string11</span>
The output should be:
<span>string1 <span>string2</span> string11</span> <span>string2</span>
What regexp is needed for this? Here is my test code:
public class Solution { public static void main(String[] args) { String testStr = "<span>string1 <span>string2</span> string11</span>"; Pattern p = Pattern.compile("(\\<(/?[^\\>]+)\\>)"); Matcher m = p.matcher(testStr); while(m.find()) { System.out.println(testStr.substring(m.start(), m.end())); } } }
And the answer is to him.
(?=((?:(?1)|<(?!/span>)|[^<]+)+</span>))
But I can not "screw"
Pattern p = Pattern.compile("(?=((?:(?1)|<(?!/span>)|[^<]+)+</span>))");
writes this:
Exception in thread "main"
java.util.regex.PatternSyntaxException
: Dangling meta character '?
'near index 0?=(<(span)>(?:(?1)|<(?!/\2>)|[^<]+)+</\2>)
Tell me how to adapt the regular season for my task?
(?1)
) do not yet support. - Temka also