I have the below java string in the below format.
externalCustomerID: { \"custToken\": \"xyz\" }
I want to extract xyz value from above string.
can anyone suggest me any regex expression for that in java?
check this one
Pattern pattern = Pattern.compile("(\\w+: \\{ \"\\w+\": \")(\\w+)");
Matcher matcher = pattern.matcher("externalCustomerID: { \"custToken\": \"xyz\" }");
if (matcher.find()) {
System.out.println(matcher.group(2));
}
Related
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.
Here is the code for my method:
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");
}
But it gives me failed in return!
Method 1
You just need to use replaceAll method in String, no need to compile a pattern and complicate things:
String input = "https://www.instagram.com/p/CANseIqFMnf/";
String output = input.replaceAll("https://www.instagram.com/p/", "").replaceAll("/", "");
Log.v(TAG, output);
Note that the first replaceAll is to remove the url and the second replaceAll is to remove any slashes /
Method 2
Pattern pattern = Pattern.compile("https://www.instagram.com/p/(.*?)/");
Matcher matcher = pattern.matcher("https://www.instagram.com/p/CANseIqFMnf/");
while(matcher.find()) {
System.out.println(matcher.group(1));
}
Note that if matcher.find() returns true then if you used modifiers like this in your REGEX (.*?) then the part found there will be in group(1), and group(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
public class Url {
public static void main(String[] args) {
String url = "https://www.instagram.com/p/CANseIqFMnf/";
String name = java.nio.file.Paths.get(url).getFileName().toString();
System.out.println(name);
}
}
This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 3 years ago.
I need to find the word "best" in a string using regex but it's throwing a "no match found" error. What am I doing wrong?
Pattern pattern = Pattern.compile("(best)");
String theString = "the best of";
Matcher matcher = pattern.matcher(theString);
matcher.matches();
String whatYouNeed = matcher.group(1);
Log.d(String.valueOf(LOG), whatYouNeed);
As per your requirement you have to find the string "best" in "the best of", so find() method suits your requirement instead of matches(). Please find the sample code snippet below:
Pattern pattern = Pattern.compile("best");
String theString = "the best of";
Matcher matcher = pattern.matcher(theString);
if(matcher.find()) {
System.out.println("found");
}else {
System.out.println("not found");
}
}
Use find() not matches!
public static void main(String[] args){
Pattern pattern = Pattern.compile("(best)");
String theString = "the best of";
Matcher matcher = pattern.matcher(theString);
if(matcher.find())
System.out.println("Hi!");
}
What I think you want is this.
String theString = "the best of";
String regex = "(best)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(theString);
while (m.find()) {
String result = m.group(1);
System.out.println("found: " + result);
}
outputs:
found: best
I have the following String line:
dn: cn=Customer Management,ou=groups,dc=digitalglobe,dc=com
I want to extract just this from the line above: Customer Management
I've tried the following RegEx expression but it does quite do what I want:
^dn: cn=(.*?),
Here is the java code snippet that tests the above expression:
Pattern pattern = Pattern.compile("^dn: cn=(.*?),");
String mydata = "dn: cn=Delivery Admin,ou=groups,dc=digitalglobe,dc=com";
Matcher matcher = pattern.matcher(mydata);
if(matcher.matches()) {
System.out.println(matcher.group(1));
} else {
System.out.println("No match found!");
}
The output is "No match found"... :(
Your regex should work properly, but matches attempts to match the regex to the entire string. Instead, use the find method which will look for a match at any point in the string.
if(matcher.find()) {
System.out.println(matcher.group(1));
} else {
System.out.println("No match found!");
}
Your problem is that the matcher want to match the whole input. Try adding a wildcard to the end of the pattern.
Pattern pattern = Pattern.compile("^dn: cn=(.*?),.*");
String mydata = "dn: cn=Delivery Admin,ou=groups,dc=digitalglobe,dc=com";
Matcher matcher = pattern.matcher(mydata);
if(matcher.matches()) {
System.out.println(matcher.group(1));
} else {
System.out.println("No match found!");
}
Please use below code:
#NOTE: instead of using matches you have to use find
public static void main(String[] args)
{
Pattern pattern = Pattern.compile("^dn: cn=(.*?),");
String mydata = "dn: cn=Delivery Admin,ou=groups,dc=digitalglobe,dc=com";
Matcher matcher = pattern.matcher(mydata);
if(matcher.find()) {
System.out.println(matcher.group(1));
} else {
System.out.println("No match found!");
}
}
I use regex and string replaceFirst to replace the patterns as below.
String xml = "<param>otpcode=1234567</param><param>password=abc123</param>";
if(xml.contains("otpcode")){
Pattern regex = Pattern.compile("<param>otpcode=(.*)</param>");
Matcher matcher = regex.matcher(xml);
if (matcher.find()) {
xml = xml.replaceFirst("<param>otpcode=" + matcher.group(1)+ "</param>","<param>otpcode=xxxx</param>");
}
}
System.out.println(xml);
if (xml.contains("password")) {
Pattern regex = Pattern.compile("<param>password=(.*)</param>");
Matcher matcher = regex.matcher(xml);
if (matcher.find()) {
xml = xml.replaceFirst("<param>password=" + matcher.group(1)+ "</param>","<param>password=xxxx</param>");
}
}
System.out.println(xml);
Desired O/p
<param>otpcode=xxxx</param><param>password=abc123</param>
<param>otpcode=xxxx</param><param>password=xxxx</param>
Actual o/p (Replaces the entire string in a single shot in first IF itself)
<param>otpcode=xxxx</param>
<param>otpcode=xxxx</param>
You need to do a non-greedy regex:
<param>otpcode=(.*?)</param>
<param>password=(.*?)</param>
This will match up to the first </param> not the last one...
Now I have this:
String s = "1<script type='text/javascript'>2</script>3<script type='text/javascript'>3</script>5";
Pattern pattern = Pattern.compile("<script.*</script>");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
s = s.replace(matcher.group(), "");
}
System.out.println(s);
The result is
15
But I need
135
In PHP we have /U modificator, but what should I do in Java? I thought about sth like this, but it is incorrect:
Pattern pattern = Pattern.compile("<script[^(script)]*</script>");
<script([^>]*)?>.*?<\/script>
Try this.You needed a ? for lazy match or shorter match.
See demo.
http://regex101.com/r/kO7lO2/3
replaceAll the below regex by empty string:
<script [^>]*>[^<]*</script>