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)
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
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!
Is it possible to attach one of a String contents to regex pattern?
Something like this, where it's need to output a Page12 from ramPage1220
String a="12"
String rp=#"[A-Z]+[a-z]+"+a
String a1="ramPage1220"
foreach(Match m in Regex.Matches(a1,rp))
{
Console.WriteLine(m.NextMatch().ToString())
}
I am not too sure about the regex you have used. But it is possible to attach one of the string content to the pattern, it will work fine. I just tested this on .NET Fiddler - https://dotnetfiddle.net/1wuj7m
and your regex should change to this
String rp=#"[a-zA-Z]+"+a;
Tested that on RegExr - http://www.regexr.com/38svb
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.
I want to check if a string contains a URL.
var string:String = "Arbitrary amount of random text before and after.<br/><br/>http://www.somdomain.co.uk<br/><br/>Tel: 0123 456 789<br/>";
var pattern:RegExp = new RegExp("(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])?");
ExternalInterface.call('console.log',pattern.test(string));
This outputs false to my console, whereas when I feed the regex and string into http://gskinner.com/RegExr/, the url is found.
What am I doing wrong and how do I fix it?
I just escaped a couple extra / and it started working. Also, I generally like to use the regex literal notation to create regexes as it gives you better syntax highlighting than converting a string to a regex in the new Regex():
var pattern:RegExp = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:\/~\+#]*[\w\-\#?^=%&\/~\+#])?/;