RegEx Advanced: Positive lookbehind

This is my test line:

<img rel="{objectid:498,newobject:1,fileid:338}" width="80" height="60" align="left" src="../../../../files/jpg1/Desert1.jpg" alt="" />

I want each JSON to form Elements between the rel attribute. It works for the first element (objectid).

Here is my ReqEx that works great:

(?<=(rel="\{objectid:))\d+(?=[,|\}])

But I want to do something like this that doesn't work:

(?<=(rel="\{.*objectid:))\d+(?=[,|\}])

So, I can parse every element of the search string.

I am using Java-ReqEx

+5
source share
3 answers

Java (and almost all regex flavors except .NET and JGSoft) do not support infinite repetition inside lookbehinds.

Instead, you can use capture groups. It is also best to use [^{]*instead .*and provide word boundaries with \b.

rel="\{[^{]*\bobjectid:(\d+)

( 1 .

+2

/? lookbehind:

String s = 
    "<img rel=\"{objectid:498,newobject:1,fileid:338}\" " +
    "width=\"80\" height=\"60\" align=\"left\" " +
    "src=\"../../../../files/jpg1/Desert1.jpg\" alt=\"\" />";
Pattern p = Pattern.compile(
    "(?:\\brel=\"\\{|\\G,)(\\w+):(\\w+)");
Matcher m = p.matcher(s);
while (m.find())
{
  System.out.printf("%s = %s%n", m.group(1), m.group(2));
}

find() rel="{. (\G,) , . (\w+):(\w+), /, - rel.

, IMG, , HTML . , , . , ([^:]+):([^,}]+) (\w+):(\w+).

+1

Lookaheads lookbehind : ( Javas) , , , *.

lookaheads lookbehinds? , .

rel="\{.*objectid:(\d+)

.

0

All Articles