Java RegExp ViewState
I am porting some functionality from a C++ application to java. This involves reading non-modifiable data files that contain regular expressions.
A lot of the data files contain regular expressions that look similar to the following:
(?<=id="VIEWSTATE".*?value=").*?(?=")
These regular expressions produce the following error:
"Look-behind group does not have an obvious maximum length near index XX"
In C++ the eng开发者_JS百科ine being used supported these expressions. Is there another form of regexp that can produce the same result that can be generated using expressions like my example as input?
As far as I know, only .NET and JGSoft, among all the current regex flavors, support unbounded quantifiers in lookbehind expressions. If you can't change the regex, you can't do what you want in Java.
But lookbehind is the wrong way to do that job in the first place. It would have been much easier, as well as more efficient, to use a capturing group:
id="VIEWSTATE".*?value="([^"]*)"
...then you retrieve the value from group #1. Are you sure you can't change the regexes?
The only workaround seems to be: Replace the star with {0,ALMOST_INTEGER_MAX_VALUE}
, where the upper limit can be large, but must be small enough for the whole lookbehind group to have a maximum length not larger than Integer.MAX_VALUE.
See also Regex look-behind without obvious maximum length in Java
精彩评论