Featured post
Infinite loop in regex in java -
my purpose match kind of different urls:
url.com
my.url.com
my.extended.url.com
a.super.extended.url.com
, on...
so, decided build regex have letter or number @ start , end of url, , have infinite number of "subdomains" alphanumeric characters , dot. example, in "my.extended.url.com", "m" "my" first class of regex, "m" "com" last class of regex, , "y.", "extended." , "url." second class of regex.
using pattern , subject in code below, want find method return me false because url must not match, uses 100% of cpu , seems stay in infinite loop.
string subject = "www.association-belgo-palestinienne-be"; pattern pattern = pattern.compile("^[a-za-z0-9]\\.?([a-za-z0-9_-]+\\.?)*[a-za-z0-9]\\.[a-za-z]{2,6}"); matcher m = pattern.matcher(subject); system.out.println(" start"); boolean hasfind = m.find(); system.out.println(" finish : " + hasfind);
which prints:
start
i can't reproduce problem using regex testers.
normal ? problem coming regex ?
due java version (1.6.0_22-b04 / jvm 64 bit 17.1-b03) ?
thanks in advance helping.
it's not infinite loop. problem it's checking every possible match , not finding one. if let run gazillion years, terminate. see this article explanation of what's happening under hood.
perhaps regular expression satisfactory (it terminates on given string): ^[a-za-z0-9][a-za-z0-9_-]*(\\.[a-za-z0-9_-]+)*\\.[a-za-z]{2,6}$
(see http://ideone.com/z0rlg)
- Get link
- X
- Other Apps
Comments
Post a Comment