While getting URL from youtube, those links are as follows:
http://www.youtube.com/watch?v=oSyL8WMLSB8
http://www.youtube.com/watch?v=4ioaMXTsAv8
http://www.youtube.com/watch?v=ZYMR6Ex9REY
http://www.youtube.com/watch?v=ok_VQ8I7g6I
I need to parse it into as follows using regex in Vb.net
http://www.youtube.com/v/oSyL8WMLSB8
http://www.youtube.com/v/4ioaMXTsAv8
http://www.youtube.com/v/ZYMR6Ex9REY
http://www.youtube.com/v/ok_VQ8I7g6I
That means remove watch? and replace = with / after v.
Sorry I'm new to regex...
You could use the below regex,
^(http:\/\/(?:www\.)?(?:youtube)\.com\/)[^?]+\?(v)=(.*)$
And in the replacement part, put the below
$1$2/$3
DEMO
OR
You could use a lookbehind to match the characters which are just after to .com/ upto the first = symbol.
(?<=\.com\/).*?=
And in the replacement part, replace the matched characters with,
v/
DEMO
Regex is overkill for this, and you don't need to use one. A simple string replacement is entirely sufficient. In C# that looks like this:
string url1 = "http://www.youtube.com/watch?v=oSyL8WMLSB8";
string url2 = url1.Replace("/watch?v=", "/v/");
// url2 => "http://www.youtube.com/v/oSyL8WMLSB8"
and in vb.net it looks like this:
Dim url1 As String = "http://www.youtube.com/watch?v=oSyL8WMLSB8"
Dim url2 As String = url1.Replace("/watch?v=", "/v/")
' url2 => "http://www.youtube.com/v/oSyL8WMLSB8"
Replace
/watch\?v=/
with
"v/"
Example:
(source: gyazo.com)
View a live regex demo!
Edit:
That means remove watch? and replace = with / after v.
On that case, see the following regex replacement:
/watch?(v)=/
Replaces to:
"$1/"
Example:
(source: gyazo.com)
View a live regex demo!
Related
I have xpaths e.g( "/name:ABC/dep:HR/eid:123" ). I have input string in this format and expecting output data to be in "/ABC/HR/123".
Please share your thoughts how to use regex pattern in scala or Java.
See regex in use here
(?<=/)[^:]*:
See code in use here
object Main extends App {
val xpath = "/name:ABC/dep:HR/eid:123"
val regex = "(?<=/)[^:]*:".r
println(regex.replaceAllIn(xpath, ""))
}
Results in /ABC/HR/123
I'm trying to create a custom filter in Google Analytic to remove the query parts of the url which I don't want to see. The url has the following structure
[domain]/?p=899:2000:15018702722302::NO:::
I would like to create a regex which skips the first 12 characters (that is until:/?p=899:2000), and what ever is going to be after that replace it with nothing.
So I made this one: https://regex101.com/r/Xgbfqz/1 (which could be simplified to .{0,12}) , but I actually would like to skip those and only let the regex match whatever is going to be after that, so that I'll be able to tell in Google Analytics to replace it with "".
The part in the url that is always the same is
?p=[3numbers]:[0-4numbers]
Thank you
Your regular expression:
\/\?p=\d{3}\:\d{0,4}(.*)
Tested in Golang RegEx 2 and RegEx101
It search for /p=###:[optional:####] and capture the rest of the right side string.
(extra) JavaScript:
paragraf='[domain]/?p=899:2000:15018702722302::NO:::'
var regex= /\/\?p=\d{3}\:\d{0,4}(.*)/;
var match = regex.exec(paragraf);
alert('The rest of the right side of the string: ' + match[1]);
Easily use "[domain]/?p=899:2000:15018702722302::NO:::".substr(12)
You can try this:
/\?p\=\d{3}:\d{0,4}
Which matches just this: ?p=[3numbers]:[0-4numbers]
Not sure about replacing though.
https://regex101.com/r/Xgbfqz/1
here is a url : http://192.168.84.98/scms?F=image&FORMAT=PNG32&TRANSPARENT=true&LAYERS=show:13&BBOXSR=aaa&IMAGESR=3857&SIZE=2528,867&BBOX=-104100.7571006687,-8544.352197105065,230333.2451006686,106152.75219710357&DPI=90
how to write a javascript regex to replace the string between "&BBOXSR=" and "&"(aaa) to another string like "bbb". note the searching string("aaa") is mutative.
the hope result url string is :http://192.168.84.98/scms?F=image&FORMAT=PNG32&TRANSPARENT=true&LAYERS=show:13&BBOXSR=bbb&IMAGESR=3857&SIZE=2528,867&BBOX=-104100.7571006687,-8544.352197105065,230333.2451006686,106152.75219710357&DPI=90
Despite the fact that you don't need regex for this task, here you are the solution just for sake of REGEX exercise.
var string = " http://192.168.84.98/scms?F=image&FORMAT=PNG32&TRANSPARENT=true&LAYERS=show:13&BBOXSR=aaa&IMAGESR=3857&SIZE=2528,867&BBOX=-104100.7571006687,-8544.352197105065,230333.2451006686,106152.75219710357&DPI=90";
string = string.replace(/(BBOXSR=)(\w)+(?=&)/g, "$1red");
console.info(string)
I am trying to write a reg expression to find match of strings / code in a database.
here is some of the sample code / string which i need to remove using the regular expression.
[b:1wkvatkt]
[/b:1wkvatkt]
[b:3qo0q63v]
[/b:3qo0q63v]
[b:2r2hso9d]
[/b:2r2hso9d]
Anything that match [b:********] and [/b:********]
Anybody please help me out. Thanks in advance.
You can use the following pattern (as stated by LukStorms in the comments):
\[\/?b:[a-z0-9]+\]
If you want to replace [b:********] with <b> (and also the closing one), you can use the following snippet (here in JavaScript, other languages are similar):
var regex = /\[(\/)?b:[a-z0-9]+\]/g;
var testText = "There was once a guy called [b:12a345]Peter[/b:12a345]. He was very old.";
var result = testText.replace(regex, "<$1b>");
console.log(result);
It matches an optional / and puts it into the first group ($1). This group can then be used in the replacement string. If the slash is not found, it won't be added, but if it is found, it will be added to <b>.
I am not really good at writing regular expressions but i need to match some parts of a URL.
http://example.com/service-page/service1/sub-service2/other-service/
my attempt looks like
service-page/(.?.+?)$
and the capture looks like service1/sub-service2/other-service/
but i need only to match if there is constant service-page inside the url and to capture the last part of the url other-service
http://example.com/[service-page]/service1/sub-service2/[other-service]/
I have to confess that I don't have WP to test with, but based on working with other regex engines, I don't see why this shouldn't work:
service-page/.*?([^/]+)/?$
To capture the name of the last subfolder:
ServicePageSubFolder("http://example.com/service-page/service1/sub-service2/other-service/");
public string ServicePageSubFolder(string url)
{
Regex regex = new Regex(#"service-page.*/([^/]+)/?");
Match match = regex.Match(url);
if (match.Success)
return match.Groups[1].Value;
return null;
}
Good luck with your quest.