C# Attach content of string to regex pattern - regex

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

Related

Add a word before each extracted String

I am capturing the session id from the string. I want to add word(prefix) before the extracted session id.
Sample input: key=this is sample input; MySessionId=hhjsfd436763jhjhfdjs87787.hghht77f54; key7=jhu8787; type=raw; oldkey=jkjf8787;
I have formed the below regex to capture the MySessionId.
MySessionId=([^.]*)
I want to add a word before the extracted string like below.
Expected output:
ABCD-1234-hhjsfd436763jhjhfdjs87787
Any way to achieve this through Regular expression?
It really depends what language you're using, you'll need to find a function that replaces text in a string (usually it's called replace). It looks like you're dealing with cookies so I'll show you an example in javascript:
//$1 refers to the first group captured by the regex
//i think other languages use $1 too but you should probably check
string = string.replace(/MySessionId([^.]*)/, "ABCD-1234-$1")

Kotlin regular expression has no match

I am practicing regular expressions in Kotlin and trying to start with a simple string. However, I am not receiving any matches. I feel like I am doing it right and can't figure out the problem.
Test String:
VERSION_ID="12.2"
And what would I do this for multiple lines:
NAME="SLED"
VERSION="12-SP2"
VERSION_ID="12.2"
PRETTY_NAME="SUSE Linux Enterprise Desktop 12 SP2"
ID="sled"
ID_LIKE="suse"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:suse:sled:12:sp2"
Regex:
private val myRegex = Regex("VERSION_ID=(\\d+)\\.(\\d+)")
I am simply trying to match 12.2 from the test string.
https://regex101.com/r/9IG8cO/1
The version ID value inside your string appears to be surrounded with double quotes. Also, I suggest making the decimal portion optional, in case some versions might not have a minor version component:
private val myRegex = Regex("VERSION_ID=\"(\\d+(?:\\.\\d+)?)\"")

Regex to replace text between slash and colon in xpath

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

regex replace special string

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)

Regex to parse youtube link

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!