I have been trying to figure out a Java RegEx for some while now that would split something like the following into two pieces:
l&<6:98>9"hello:world"-45:&<78:89>"hedhed:hdeh"+56
it should be split at the colon after "-45" ignoring all colons inside tags and quotes. Neither of the sides must not necessarily contain any tags or quotes.
Help would be greatly appreciated :)
This would be a starting point for a parsing function:
/** example: findCharIndex(subject, ':'); */
public static int findCharIndex(String subject, char findChar)
{
boolean insideQuotes = false;
boolean insideTags = false;
for (int index = 0; index < subject.length(); index++)
{
char ch = subject.charAt(index);
if (ch == '"')
insideQuotes = !insideQuotes;
else if (!insideQuotes)
{
if (ch == '<')
insideTags = true;
else if (insideTags && ch == '>')
insideTags = false;
}
if (!insideQuotes && !insideTags && ch == findChar)
return index;
}
return -1;
}
执行匹配比拆分更容易。
(?:[^"<:]|"[^"]*"|<[^>]*)*
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.