extract String between last slash and question mark - android

I want to extract string between last slash and question mark using regex
String path="http://keting.amazonaws.com/media123/mediaattachments/000/000/004/original/Shoes_TVC_2013.mp4?1470248308"
I need to get "Shoes_TVC_2013.mp4" from the String.
Pattern pattern = Pattern.compile("NEED HELP HERE");
Matcher matcher = pattern.matcher(URL);
if (matcher.find()) {
System.out.println(matcher.group(1)); //prints region/country
} else {
System.out.println("Match not found");
}

.*\/(.*)\? should do the trick. See here for more.

Try this solution,
String path="http://keting.amazonaws.com/media123/mediaattachments/000/000/004/original/Shoes_TVC_2013.mp4?1470248308";
path = path.substring(path.lastIndexOf("/")+1,path.lastIndexOf("?"));

Related

regex skips the first match

The regex
.*([0-9]{3}\\.[0-9]{2}).*
finds one match in "some short sentence 111.01 ", but it failed to match the first occurrence "111.01" in "some short sentence 111.01 & 222.02 "
I tried the lazy quantifier .*([0-9]{3}\\.[0-9]{2})?.* or .*([0-9]{3}\\.[0-9]{2}).*? for no avail.
Please help, I need to get both occurrences, here is my code.
Thank you
Pattern myPattern = Pattern.compile(".*([0-9]{3}\\.[0-9]{2}).*");
Matcher m = myPattern.matcher(mystring);
while (m.find()) {
String found = m.group(1);
}
you need to remove ".*"s. Try this:
String mystring = "some short sentence 111.01 & 222.02 ";
Pattern myPattern = Pattern.compile("([0-9]{3}\\.[0-9]{2})");
Matcher m = myPattern.matcher(mystring);
while(m.find()) {
System.out.println("Found value: " + m.group(1) );
}
output:
Found value: 111.01
Found value: 222.02
The leading and trailing ".*" cause you to match the entire string in one match. All the lazy quantifier does in your case is controls you getting the first, not last, occurrence in the subject.

how to get text with using substring

I try to get only this part "9916-4203" in "Region Code:9916-4203 " in android. How can I do this?
I tried below code, I used substring method but it doesn't work:
firstNumber = Integer.parseInt(message.substring(11, 19));
If you know that string contains "Region Code:" couldn't you do a replace?
message = message.replace("Region Code:", "");
Assumed that you have only one phone number in your String, the following will remove any non-digit characters and parse the resulting number:
public static int getNumber(String num){
String tmp = "";
for(int i=0;i<num.length();i++){
if(Character.isDigit(num.charAt(i)))
tmp += num.charAt(i);
}
return Integer.parseInt(tmp);
}
Output in your case: 99164203
And as already mentioned, you won't be able to parse any String to Integer in case there are any non-digit characters
Im going to guess that what you want to extract is the full region code text minus the title. So maybe using regex would be a good simple fit for you?
String myString = "Region Code:9916-4203";
String match = "";
String pattern = "\:(.*)";
Pattern regEx = Pattern.compile(pattern);
Matcher m = regEx.matcher(myString);
// Find instance of pattern matches
Matcher m = regEx.matcher(myString);
if (m.find()) {
match = m.group(0);
}
Variable match will contain "9916-4203"
This should work for you.
Java code sourced from http://android-elements.blogspot.in/2011/04/regular-expressions-in-android.html
In Java the substring() method works with the first parameter being inclusive and the second parameter being exclusive. Meaning "Hello".substring(0, 2); will result in the string He.
In addition to excluding the parsing of something that isn't a number like #Opiatefuchs mentioned, your substring method should instead be message.substring(12, 21).

How do I match the pattern in android

I am currently working on an android project. I would like to know if a string contains the '\r' or '\n' as the last character. How to do the pattern match?
Try this.
String string = "stackoverflow";
lastchar = string.substring(string.length() - 1);
It will give you the result "w".
try
String patterntomatch ="^[_A-Za-z0-9-]*(\r\n)$";
Pattern pattern=Pattern.compile(patterntomatch);
Matcher matcher=pattern.matcher(matchfromedittext);
boolean matcher.matches();
String last = your_string.substring(Math.max(your_string.length() - 2, 0));
//It will give you the last 2 characters of the string. If the string is null
//or has less than 2 characters, then it gives you the original string.
if(last.equals("\r") || last.equals("\n")){
//String's last two characters are either \n or \r. Now do something.
}

Android and regex to read substring

I'm trying to use regex in an Android application to read a substring. I'm using Pattern and Matcher, but I can't figure out how to do it.
My input string is: javascript:submitForm(document.voip_call_log,30,0,'xxxxx','',0,'');
where xxxxx is a variable number of digits.
How can I read 'xxxxx' using Pattern and Matcher?
I'm not an expert in RegEx, but this one should work for you:
String input = "javascript:submitForm(document.voip_call_log,30,0,'5555','',0,'');";
Pattern pattern = Pattern.compile(",'(\\d*)',");
Matcher matcher = pattern.matcher(input);
matcher.find();
String out = matcher.group(1);
System.out.println(out);
Proof http://ideone.com/WI6VB1
If you will have always that format, it might be easier to use the split(",") method and access the value you like.
Alternatively, you could try something like so:
String str = ...;
Pattern p = Pattern.compile("(\\d{3,})");
Matcher m = p.matcher(str);
while(m.find())
{
System.out.println(m.group(1));
}
Try this one:
String s = "javascript:submitForm(document.voip_call_log,30,0,'1999','',0,'');";
Pattern pattern = Pattern.compile("javascript:submitForm\\([^,]+,\\d*,\\d*,'(\\d+)','\\d*'");
Matcher m = pattern.matcher(s);
if(m.find( )) {
System.out.println(m.group(1));
}
This is following the pattern of your input string from the question description. You can find the explanation of your regex from this link. Just input your regex with single slash \ instead of double \\

How to get data between apostrophes Android

I am trying to get substrings from the string which are between apostrophes using regex.
Format of the string: Duplicate entry 'bla#bla.bl' for key 'email'.
The regex I am using: '([^']*).
Code:
Pattern pattern = Pattern.compile("'([^']*)");
Matcher matcher = pattern.matcher(duplicated);
Log.d(TAG, matcher.group()));
I am not also sure about matcher.group(), which returns a single string, that matched the whole regex. In my case, it should return two substrings.
Can somebody correct this regex and give me an explanation?
Thanks in advance
Better to use .split() instead of Pattern Matching. Its simply hard-coding. Do as below:
String[] strSplitted = <Your String>.split("`");
Then, the strSplitted Array contains the Strings splitted between `.
I would use this regex. It is almost exactly like yours but I include the closing single quote. This is to prevent the closing single quote from being used in the next match.
'([^']*)'
And to get the contents inside the single quotes use a line similar to this:
matcher.group(1)
Here is a Java example:
Pattern regex = Pattern.compile("'([^']*)'", Pattern.MULTILINE);
Matcher matcher = regex.matcher(duplicated);
while (matcher.find()) {
Log.d(TAG, matcher.group(1)));
}
Here's my tested solution. You have to call find
Pattern pattern = Pattern.compile("'([^']*)'");
String duplicated = "Duplicate entry 'bla#bla.bl' for key 'email'";
Matcher matcher = pattern.matcher(duplicated);
String a = "";
while (matcher.find()) {
a += matcher.group(1) + "\n";
}
Result:
bla#bla.bl
email
I invent my solution like following.
int second_index = 0;
String str = "Duplicate entry 'bla#bla.bl' for key 'email'";
while (true) {
if (second_index == 0)
first_index = str.indexOf("'", second_index);
else
first_index = str.indexOf("'", second_index + 1);
if (first_index == -1)
break;
second_index = str.indexOf("'", first_index + 1);
if (second_index == -1)
break;
String temp = str.substring(first_index + 1, second_index);
Log.d("TAG",temp);
}
Output
06-25 17:25:17.689: bla#bla.bl
06-25 17:25:17.689: email

Categories

Resources