url2 = "http://ggg.pythonchallenge.net/pc/def/abc123.php"
url3 = "http://rrr.pythonchallenge.gov/pc/def/abc123.jsp"
Q1:我想获得adb123如何用re实现呢?
Q2:如果我想获取pythonchallenge如何实现呢?
Q3:如果我想获取pc 或者 def如何实现呢?
好像挺有难度的哦
3 个解决方案
#1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test{
public static void main(String[] args){
String url1 = "http://www.pythonchallenge.com/pc/def/abc123.html";
String url2 = "http://ggg.pythonchallenge.net/pc/def/abc123.php";
String url3 = "http://rrr.pythonchallenge.gov/pc/def/abc123.jsp";
process1(url1);
System.out.println("-------------------------------------------");
process2(url2);
System.out.println("-------------------------------------------");
process3(url3);
}
private static void process1(String url){
String regex = "(?<=/)(?<content>[^/.]+)[.]\\w+$";
Matcher matcher = Pattern.compile(regex).matcher(url);
if(matcher.find()){
System.out.println(matcher.group("content"));
}
}
private static void process2(String url){
String regex = "(?<=[.])(?<content>[^.]+)[.]\\w+(?=/)";
Matcher matcher = Pattern.compile(regex).matcher(url);
if(matcher.find()){
System.out.println(matcher.group("content"));
}
}
private static void process3(String url){
String regex = "(?<=/)(?<content>\\w+)(?=/)";
Matcher matcher = Pattern.compile(regex).matcher(url);
while(matcher.find()){
System.out.println(matcher.group("content"));
}
}
}
#2
感谢您的回答,很正确,您所用的是?<content>是re采集器吗,能用在Python上吗?
还有个问题:
//为啥我定义2个regex相同的会报错呢?
private static void process1(String url){
String regex = "(?<=/)(?<content>[^/.]+)[.]\\w+$";
String regex2 = "(?<=/)(?<content>[^/.]+)[.]\\w+$";
Matcher matcher = Pattern.compile(regex).matcher(url);
Matcher matcher2 = Pattern.compile(regex2).matcher(url);
if(matcher.find()){
System.out.println("p1:"+matcher.group("content"));
System.out.println("pm2:"+matcher2.group("content"));
}
}
p1:abc123
Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:524)
at com.xzh.re.Test.process1(Test.java:26)
at com.xzh.re.Test.main(Test.java:12)
#3
(?<>)这个是在java1.7后引用的.至于你说的Python没学过,你可以试下.
至于你那个报错的问题.改成这样就没问题了.因为java中正则的匹配存在一个指针的概念,每次匹配都会改变起始位置,如果你没调用find()方法的那么就会报错.
至于你那个报错的问题.改成这样就没问题了.因为java中正则的匹配存在一个指针的概念,每次匹配都会改变起始位置,如果你没调用find()方法的那么就会报错.
private static void process1(String url){
String regex = "(?<=/)(?<content>[^/.]+)[.]\\w+$";
String regex2 = "(?<=/)(?<content>[^/.]+)[.]\\w+$";
Matcher matcher = Pattern.compile(regex).matcher(url);
Matcher matcher2 = Pattern.compile(regex2).matcher(url);
if(matcher.find()){
System.out.println("p1:"+matcher.group("content"));
}
if(matcher2.find()){
System.out.println("pm2:"+matcher2.group("content"));
}
}
#1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test{
public static void main(String[] args){
String url1 = "http://www.pythonchallenge.com/pc/def/abc123.html";
String url2 = "http://ggg.pythonchallenge.net/pc/def/abc123.php";
String url3 = "http://rrr.pythonchallenge.gov/pc/def/abc123.jsp";
process1(url1);
System.out.println("-------------------------------------------");
process2(url2);
System.out.println("-------------------------------------------");
process3(url3);
}
private static void process1(String url){
String regex = "(?<=/)(?<content>[^/.]+)[.]\\w+$";
Matcher matcher = Pattern.compile(regex).matcher(url);
if(matcher.find()){
System.out.println(matcher.group("content"));
}
}
private static void process2(String url){
String regex = "(?<=[.])(?<content>[^.]+)[.]\\w+(?=/)";
Matcher matcher = Pattern.compile(regex).matcher(url);
if(matcher.find()){
System.out.println(matcher.group("content"));
}
}
private static void process3(String url){
String regex = "(?<=/)(?<content>\\w+)(?=/)";
Matcher matcher = Pattern.compile(regex).matcher(url);
while(matcher.find()){
System.out.println(matcher.group("content"));
}
}
}
#2
感谢您的回答,很正确,您所用的是?<content>是re采集器吗,能用在Python上吗?
还有个问题:
//为啥我定义2个regex相同的会报错呢?
private static void process1(String url){
String regex = "(?<=/)(?<content>[^/.]+)[.]\\w+$";
String regex2 = "(?<=/)(?<content>[^/.]+)[.]\\w+$";
Matcher matcher = Pattern.compile(regex).matcher(url);
Matcher matcher2 = Pattern.compile(regex2).matcher(url);
if(matcher.find()){
System.out.println("p1:"+matcher.group("content"));
System.out.println("pm2:"+matcher2.group("content"));
}
}
p1:abc123
Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:524)
at com.xzh.re.Test.process1(Test.java:26)
at com.xzh.re.Test.main(Test.java:12)
#3
(?<>)这个是在java1.7后引用的.至于你说的Python没学过,你可以试下.
至于你那个报错的问题.改成这样就没问题了.因为java中正则的匹配存在一个指针的概念,每次匹配都会改变起始位置,如果你没调用find()方法的那么就会报错.
至于你那个报错的问题.改成这样就没问题了.因为java中正则的匹配存在一个指针的概念,每次匹配都会改变起始位置,如果你没调用find()方法的那么就会报错.
private static void process1(String url){
String regex = "(?<=/)(?<content>[^/.]+)[.]\\w+$";
String regex2 = "(?<=/)(?<content>[^/.]+)[.]\\w+$";
Matcher matcher = Pattern.compile(regex).matcher(url);
Matcher matcher2 = Pattern.compile(regex2).matcher(url);
if(matcher.find()){
System.out.println("p1:"+matcher.group("content"));
}
if(matcher2.find()){
System.out.println("pm2:"+matcher2.group("content"));
}
}