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+)?)\"")
Related
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>.
We are currently matching "service_hub*queue"
I want to ignore the case "service_hub_scout_dead_queue" and yet still match everything else.
What is the regular expression for that ?
This javascript sollution gives an array with the matches
var myText = 'service_hub_anything_queue Add service_hub_scout_dead_queue something service_hub_someting_queue else';
var myMatches = myText.match(/service_hub(?!_scout_dead_)\w+queue/g);
If you are rather interested in what follows a match
var mySplit = ('dummy'+myText).split(/service_hub(?!_scout_dead_)\w+queue/g).filter(function(txt,i) {return (i>0);})
I put 'dummy' and then filter away the first part to make it work both if the sting starts with a valid tag and when it does not.
Using negative lookbehind: "service_hub_.*?(?<!_scout_dead)_queue"
This appears to be widely supported by popular regex engines; I've tested with Java (or Scala, rather) just to make sure it works.
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 need to validate a regular expression aganist a string veryt strictly only what is there in string. But at present i don't see such a function for regular expression since bot exec and test doesn't check strictly. for example say i have regular expression for validating 43a^2b^2, but even its 43a it's returning true, as a part of string is present in reg exp. i need true only when exact match is found, is there any way achieve this?
The code I am using at present:
var isRegExp:Boolean = regExp.test(value1);
Thanks in advance...
Using the beginning of string ^ and end of string $ tests should work.
var regExp:RegExp = /^43a$/;
regExp.test('43a'); //true
regExp.test('43a^2b^2'); //false
You can read more about anchors here.
I want to find and replace a sort of repetitive strings in my project.
I want to use the VS2012 find and replace to make my task easy.
The original string is
Format(Element[Be], Element[Be])
and I want to replace it following
Element[Be].ToString(Element[Be].Value)
How that can be done using FindAndReplaace feature of VS2012 using REGEX
Try the following regex:
Format\(([^,]*),\s*([^\)]*)\)
and the following replacement string:
$1.ToString($2.Value);
in C#:
var input = "Format(Element[Be], Element[Be])";
var result = Regex.Replace(input, #"Format\(([^,]*),\s*([^\)]*)\)", "$1.ToString($2.Value)");
Regex101 Demo