[英]convert php regex to android java regex
Can somebody help me with converting a PHP regex to Java format? 有人可以帮我将PHP正则表达式转换为Java格式吗?
It would be great and I would appreciate if anyone could help me, because I'm not so strong in regex. 太好了,如果有人可以帮助我,我将不胜感激,因为我对正则表达式的了解不强。
This is a PHP code: 这是一个PHP代码:
$string = file_get_contents('a.txt');
$regex = '/\b(http|https):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i';
preg_match_all($regex, $string, $matches);
$urls = $matches[0];
You should double escape the back slashes and you do not have to escape /
s: 你应该加倍逃脱反斜线和你没有逃跑/
S:
String rx = "(?i)\\b(http|rtmp|rtsp|mmsh|mms)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]";
Sample code to get all matches (do not forget about including import java.util.regex.*;
): 获取所有匹配项的示例代码(不要忘记包含import java.util.regex.*;
):
String str = "http://google.com";
String rx = "(?i)\\b(http|rtmp|rtsp|mmsh|mms)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]";
Pattern ptrn = Pattern.compile(rx);
Matcher m = ptrn.matcher(str);
while (m.find()) {
System.out.println(m.group(0));
}
Basic Regex syntax are same in all language Just be careful around the escape character things. 所有语言中的Regex基本语法都是相同的。只是请小心转义字符。 To escape special characters use \\\\
, there is no need to escape /
要转义特殊字符,请使用\\\\
,无需转义/
and some parameter will be given in a different way in java like. 并且某些参数将以类似Java的方式给出。
use (?i)
at the beginning instead of /i
at the end 在开头使用(?i)
,而不在结尾使用/i
boundary expression \\b
also same in java but you will need to add \\\\b
in java because \\b
will be considered as a backspace back-space character 边界表达式\\b
在Java中也相同,但是您需要在Java中添加\\\\b
,因为\\b
将被视为退格退格字符
Your pattern will look like: 您的模式将如下所示:
(?i)\\\\b(http|rtmp|rtsp|mmsh|mms)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.