I'm trying to extract CANseIqFMnf
from the URL https://www.instagram.com/p/CANseIqFMnf/
using regex in Android studio. Please help me to get a regex expression eligible for Android Studio.
这是我的方法的代码:
String url = "https://www.instagram.com/p/CANseIqFMnf/";
String REGEX = "/p\//";
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(url);
boolean match = matcher.matches();
if (match){
Log.e("success", "start = " + matcher.start() + " end = " + matcher.end() );
}else{
Log.e("failed", "failed");
}
但这给了我失败!
方法1
You just need to use
replaceAll
method inString
, no need to compile a pattern and complicate things:Note that the first
replaceAll
is to remove theurl
and the secondreplaceAll
is to remove any slashes/
方法2
Note that if
matcher.find()
returnstrue
then if you used modifiers like this in your REGEX(.*?)
then the part found there will be ingroup(1)
, andgroup(0)
will hold the entire regex match which is in your case the entire url.Alternate option w/o regex can be implemented in a simpler manner as below using
java.nio.file.Paths
APIs