regex - Java regular expression is not matching valid results -
this question has answer here:
i'm trying write simple java regular expression extract out video id of given youtube video url. e.g for:
http://www.youtube.com/watch?v=-mzvaauco1c
i want extract out: -mzvaauco1c
.
here's i'm trying:
pattern pattern = pattern.compile("v=([^&]+)"); string url = "http://www.youtube.com/watch?v=-mzvaauco1c"; matcher matcher = pattern.match(url); system.out.println(matcher.getgroupcount() ); //outputs 1 system.out.println(matcher.matches() ); //returns false; system.out.println( matcher.group(0) ); //throws exception, same 1
what doing wrong?
invoke find
match partial string
. dont call matches
after calling find
- result in illegalstateexception
. want capture group 1
rather 0
latter returns full string
pattern pattern = pattern.compile("v=([^&]+)"); string url = "http://www.youtube.com/watch?v=-mzvaauco1c&foo=3"; matcher matcher = pattern.matcher(url); if (matcher.find()) { system.out.println(matcher.groupcount()); system.out.println(matcher.group(1)); }
Comments
Post a Comment