简体   繁体   中英

Flex RegExp to Java RegExp

Recently i was looking for help to extract some pattern of a URL using RegExp in flex, Few guys kindly helped me to get it done. See this I need to do it again in java layer, i tried to applied the same regExp pattern in java, but it gives me an error.

var s:String = "www.something.com@param1={{^User Name^}},{{^,e,^}},param2={{^user id^}}";
var userPattern:RegExp = /(?<=param1=)({{\^)([A-Za-z0-9\^}}{{, ])*(})/;
var userIdPattern:RegExp = /(?<=param2=)({{\^)([A-Za-z0-9\^}}{{, ])*(})/;
s = s.replace(userPattern,'username');
s = s.replace(userIdPattern,'user_id');

This gives the www.something.com@param1=username,param2=user_id.

Can somebody help me out to do this in java layer, relavent regular expression is enough.

This is what i tried in java,

String url = "http://www.google.com/@param1={{^Case Name^}},param2={{^Case Id^}}";
String urlPattern = "/({{\\^)([A-Za-z0-9_\\^}}{{, ])*(})/";
Pattern paramPattern = Pattern.compile(urlPattern);     
Matcher matcher = paramPattern.matcher(url);       
System.out.println("Matches - "+matcher.matches());
System.out.println("Find - "+matcher.find());

It gives this error

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 1 /({{\\^)([A-Za-z0-9_\\^}}{{, ])*(})/

{ is a meta character in Java regexes, for bounded repetition, eg a{1,2} matches at least 1, at most 2 'a's. As you are not using it for repetition, you ought to escape each { with \\\\ .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM