Example URL : https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw
I need only UCAoMPWcQKA_9Af5YhWdrZgw (Channel ID)
Subhendu Mondal decision crashes on links like this.
// args after id
https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw?view_as=subscriber
//minus sign in id
https://www.youtube.com/channel/UCQ18THOcZ8nIP-A3ZCqqZwA
so here is improved decision, also protocol group was removed from match result
^(?:http|https):\/\/[a-zA-Z-]*\.{0,1}[a-zA-Z-]{3,}\.[a-z]{2,}\/channel\/([a-zA-Z0-9_\-]{3,24})
var str = "https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw";
var pattern = /^(?:(http|https):\/\/[a-zA-Z-]*\.{0,1}[a-zA-Z-]{3,}\.[a-z]{2,})\/channel\/([a-zA-Z0-9_]{3,})$/;
var match = str.match(pattern);
console.log(match[1]);
UPDATE:
Here is the shortest way:
youtube.com\/channel\/([^#\&\?]*).*
Parse the string into a URI:
Uri uri = Uri.parse("https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw")
String id = uri.lastPathSegment() //UCAoMPWcQKA_9Af5YhWdrZgw
This is the pattern you can use to capture the channel id and this will validate the url also
^(?:(http|https):\/\/[a-zA-Z-]*\.{0,1}[a-zA-Z-]{3,}\.[a-z]{2,})\/channel\/([a-zA-Z0-9_]{3,})$
I have no idea how to execute regex in android but sharing the regex url, you can check from here https://regex101.com/r/9sjMPp/1
Or a javascript code to perform
var str = "https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw";
var pattern = /^(?:(http|https):\/\/[a-zA-Z-]*\.{0,1}[a-zA-Z-]{3,}\.[a-z]{2,})\/channel\/([a-zA-Z0-9_]{3,})$/;
var matchs = str.match(pattern);
console.log(matchs[2]);
// output is UCAoMPWcQKA_9Af5YhWdrZgw
Hope you get the idea.
You can use this regex for retrieving video ids from different types of youtube url.
String pattern = "(?<=watch\\?v=|/videos/|embed\\/|youtu.be\\/|\\/v\\/|\\/e\\/|watch\\?v%3D|watch\\?feature=player_embedded&v=|%2Fvideos%2F|embed%\u200C\u200B2F|youtu.be%2F|%2Fv%2F)[^#\\&\\?\\n]*";
Then generate a Pattern instance using the above regex, from which you can get a Matcher instance by using Pattern.matcher(videoLink).
Now the Matcher.find() will give you a boolean on whether the above regex occurs in the given video link or not.
Depending on that you can use Matcher.group(), which will give you back the video id.
Refer Here
Related
I have strings in my strings.xml e.g.:
<string name="category__service">Service</string>
I want to access them like this:
val key = "category__$this.name" // "category__service"
val s = R.string.[key]
This would give me the Id of the string which I can use.
But this way I get the error
The expression cannot be a selector (occur after a doted text)
I also tried
val s = R.string.$key
but I get:
Expecting an element
The documentation on what R is to begin with, isn't giving me much. As far as I see – R.string does not have a simple getter.
So at this point I'm just guessing for a solution. Is this even possible in Kotlin?
You can try following:
val key = "category__$this.name" // "category__service"
val s = resources.getIdentifier(key, "string", context.packageName)
I am developing a Weather app ,in that trying to build Uri that looks like
Content://com.example.weather.app/Location/locationName?Date=12012017
The documentation says Uri reference has pattern as ://?#
trying to understand this following code
public static Uri buildWeatherLocation(String locationSetting) {
return CONTENT_URI.buildUpon().appendPath(locationSetting).build();
}
public static Uri buildWeatherLocationWithStartDate(
String locationSetting, long startDate) {
long normalizedDate = normalizeDate(startDate);
return CONTENT_URI.buildUpon().appendPath(locationSetting)
.appendQueryParameter(COLUMN_DATE,Long.toString(normalizedDate)).build();
}
what is the actual difference and when we use appendPath() and appendQueryParameter() methods?
why can't we use appendQueryParameter() for locationSetting ,bit confusing suggestions plz
appendPath() is for path segments and appendQueryParameter() for query params with key value (in your example Date=12012017).
Check this link for more info and examples:
Use URI builder in Android or create URL with variables
appendQueryParameter is for query string parameters and appendPath is for site path
I want to retrieve few characters from string i.e., String data on the basis of first colon (:) used in string . The String data possibilities are,
String data = "smsto:....."
String data = "MECARD:....."
String data = "geo:....."
String data = "tel:....."
String data = "MATMSG:....."
I want to make a generic String lets say,
String type = "characters up to first colon"
So i do not have to create String type for every possibility and i can call intents according to the type
It looks like you want the scheme of a uri. You can use Uri.parse(data).getScheme(). This will return smsto, MECARD, geo, tel etc...
Check out the Developers site: http://developer.android.com/reference/android/net/Uri.html#getScheme()
Note: #Alessandro's method is probably more efficient. I just got that one off the top of my head.
You can use this to get characters up to first ':':
String[] parts = data.split(":");
String beforeColon = parts[0];
// do whatever with beforeColon
But I don't see what your purpose is, which would help giving you a better solution.
You should use the method indexOf - with that you can get the index of a certain char. Then you retrieve the substring starting from that index. For example:
int index = string.indexOf(':');
String substring = string.substring(index + 1);
I've got a HTML code stored in string and I want to extract all parts that match the pattern, which is:
<a href="http://abc.pl/(.*?)/(.*?)"><img src="(.*?)"
(.*?) stands for any string. I've tried dozens of combinations and couldn't get it working. Can somebody show me a sample code, which extracts all matched data from a String and store it in variables?
Thanks in advance
Here is a solution using JavaScript. I hope this helps.
First, we need a working pattern:
var pattern = '<a href="http://abc.pl/([^/"]+)/([^/"]*)".*?><img src="([^"]*)"';
Now, the problem is that in JavaScript there is no native method or function that retrieves both all matches and all submatches at once, whatever the regexp we use.
We can easily retrieve an array of all the full matches:
var re = new RegExp(pattern, "g");
var matches = yourHtmlString.match(re);
But we also want the submatches, right? In my humble opinion, the simplest way to achieve this is to apply the non-greedy version of the same regexp to each match we obtained (because only non-greedy regexes can return submatches):
var reNonGreedy = new RegExp(pattern);
var matchesAndSubmatches = [];
for(var i = 0; i < matches.length; i++) {
matchesAndSubmatches[i] = matches[i].match(reNonGreedy);
}
Each element of matchesAndSubmatches is now an array such that:
matchesAndSubmatches[n][0] is the n-th full match,
matchesAndSubmatches[n][1] is the first submatch of the n-th full match,
matchesAndSubmatches[n][2] is the second submatch of the n-th full match, and so on.
Well, here's the sample:
Pattern pattern = Pattern.compile("patternGoesHere");
Matcher matcher = pattern.matcher(textGoesHere);
while (matcher.find())
{
// You can access substring here via matcher.group(substringIndex) [note they are indexed from 1, not 0]
}
I am trying to validate my string with the regular expression. Here is what I am trying to do
EditText serialText = (EditText) findViewById(R.id.pinText);
serialText.setVisibility(View.VISIBLE);
serialNumber = serialText.getText().toString();
I am storing the serial number in serialNumber
I have the following method to match the regular expression
boolean isRegularSerialNumber(String pinNumber) {
// regular expression to be matched against
String regularString = "[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}";
Pattern pattern = Pattern.compile(regularString);
Matcher matcher = pattern.matcher(pinNumber);
boolean isRegularSerialNumberValid ;
if (pinNumber.matches(regularString))
isRegularSerialNumberValid = true;
else
isRegularSerialNumberValid = false;
return isRegularSerialNumberValid;
}
But I am not able to match this.
Any answer for this? Hope Pattern and Matcher are the right one for this.
What I am trying to do is this, this matched serialNumber I am validating against serial number stored in the database. If match found, it returns success or else failure. And i have entered the exact serial number which is stored in the database but even then it returns failure.
I followed the method what #Stevehb said and i got the match true in that case.
This is how I am sending my data
parameter.add(new BasicNameValuePair("validate", serialNumber));
Breaking my head on this.
The built in String functions should work by themselves. isRegularSerialNumber() could just be
boolean isRegularSerialNumber(String pinNumber) {
String regularString = "[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}";
return pinNumber.matches(regularString);
}
This works for me when I tested 1234-5678-9012-1324 (true) and 12-1234-123-1324 (false).
Also, it looks like you're maybe grabbing the input string from serialText right after you make it visible. Could your problem be in grabbing the text before the user has made any input?
looks much alike .net regex code.
instead of
if (pinNumber.matches(regularString))
try
if (matcher.matches())