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
Related
What would be a quick way to extract the value of the title attributes for an HTML table:
...
<li>Proclo</li>
<li>Proclus</li>
<li>Ptolemy</li>
<li>Pythagoras</li></ul><h3>S</h3>
...
so it would return Proclo, Proclus, Ptolemy, Pythagoras,.... in strings for each line. I'm reading the file using a StreamReader. I'm using C#.
Thank you.
This C# regex will find all title values:
(?<=\btitle=")[^"]*
The C# code is like this:
Regex regex = new Regex(#"(?<=\btitle="")[^""]*");
Match match = regex.Match(input);
string title = match.Value;
The regex uses positive lookbehind to find the position where the title value starts. It then matches everything up to the ending double quote.
Use the regexp below
title="([^"]+)"
and then use Groups to browse through matched elements.
EDIT: I have modified the regexp to cover the examples provided in comment by #Staffan Nöteberg
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>.
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!
I am currently working in an application where I need to find all occurrences of strings like ${[0-9-a-zA-Z]} in a bigger string. Here is my method:
def countVariables(str) {
def pattern = ~'${sss}'
def matcher = str =~ pattern
print matcher.count
}
Now the problem.
When I pass a string like "asidb ${sss} asodniasndin", I get:
groovy.lang.MissingPropertyException: No such property: sss for class: ConsoleScript83
I think that, given that in Groovy ${} are properties, I'm having these conflicts.
In this case, I would have to run the whole text searching for the dollar sign and replacing it for something else? Or is there a simpler way to do this?
Regards!
Are you using single quotes so groovy doesn't do the expansion and just gives you a string?
Ie:
countVariables( 'asidb ${sss} asodniasndin' )