In C# I'm trying to validate a string that looks like:
I#paramname='test'
or
O#paramname=2827
Here is my code:
string t1 = "I#parameter='test'";
string r = #"^([Ii]|[Oo])#\w=\w";
var re = new Regex(r);
If I take the "=\w" off the end or variable r I get True. If I add an "=\w" after the \w it's False. I want the characters between # and = to be able to be any alphanumeric value. Anything after the = sign can have alphanumeric and ' (single quotes). What am I doing wrong here. I very rarely have used regular expressions and normally can find example, this is custom format though and even with cheatsheets I'm having issues.
^([Ii]|[Oo])#\w+=(?<q>'?)[\w\d]+\k<q>$
Regular expression:
^ start of line
([Ii]|[Oo]) either (I or i) or (O or o)
\w+ 1 or more word characters
= equals sign
(?<q>'?) capture 0 or 1 quotes in named group q
[\w\d]+ 1 or more word or digit characters
\k<q> repeat of what was captured in named group q
$ end of line
use \w+ instead of \w to one character or more. Or \w* to get zero or more:
Try this: Live demo
^([Ii]|[Oo])#\w+=\'*\w+\'*
If you are being a bit more strict with using paramname:
^([Ii]|[Oo])#paramname=[']?[\w]+[']?
Here is a demo
You could try something like this:
Regex rx = new Regex( #"^([IO])#(\w+)=(.*)$" , RegexOptions.IgnoreCase ) ;
Match group 1 will give you the value of I or O (the parameter direction?)
Match group 2 will give you the name of the parameter
Match group 3 will give you the value of the parameter
You could be stricter about the 3rd group and match it as
(([^']+)|('(('')|([^']+))*'))
The first alternative matches 1 or more non quoted character; the second alternative match a quoted string literal with any internal (embedded) quotes escape by doubling them, so it would match things like
'' (the empty string
'foo bar'
'That''s All, Folks!'
Related
I'm trying to come up with a regular expression matches the text in bold in all the examples.
Between the string "JZ" and any character before "-"
JZ123456789-301A
JZ134255872-22013
Between the string "JZ" and the last character
JZ123456789D
I have tried the following but it only works for the first example
(?<=JZ).*(?=-)
You can use (?<=JZ)[0-9]+, presuming the desired text will always be numeric.
Try it out here
You may use
JZ([^-]*)(?:-|.$)
and grab Group 1 value. See the regex demo.
Details
JZ - a literal substring
([^-]*) - Capturing group 1: zero or more chars other than -
(?:-|.$) - a non-capturing group matching either - or any char at the end of the string
C# code:
var m = Regex.Match(s, #"JZ([^-]*)(?:-|.$)");
if (m.Success)
{
Console.WriteLine(m.Groups[1].Value);
}
If, for some reason, you need to obtain the required value as a whole match, use lookarounds:
(?<=JZ)[^-]*(?=-|.$)
See this regex variation demo. Use m.Value in the code above to grab the value.
A one-line answer without regex:
string s,r;
// if your string always starts with JZ
s = "JZ123456789-301A";
r = string.Concat(s.Substring(2).TakeWhile(char.IsDigit));
Console.WriteLine(r); // output : 123456789
// if your string starts with anything
s = "A12JZ123456789-301A";
r = string.Concat(s.Substring(s.IndexOf("JZ")).TakeWhile(char.IsDigit));
Console.WriteLine(r); // output : 123456789
Basically, we remove everything before and including the delimiter "JZ", then we take each char while they are digit. The Concat is use to transform the IEnumerable<char> to a string. I think it is easier to read.
Try it online
I have the following string
{token1;token2;token3#somewhere.com;...;tokenn}
I need a Regex pattern, that would give a result in array of strings such as
token1
token2
token3#somewhere.com
...
...
...
tokenn
Would also appreciate a suggestion if can use the same pattern to confirm the format of the string, means string should start and end in curly braces and at least 2 values exist within the anchors.
You may use an anchored regex with named repeated capturing groups:
\A{(?<val>[^;]*)(?:;(?<val>[^;]*))+}\z
See the regex demo
\A - start of string
{ - a {
(?<val>[^;]*) - Group "val" capturing 0+ (due to * quantifier, if the value cannot be empty, use +) chars other than ;
(?:;(?<val>[^;]*))+ - 1 or more occurrences (thus, requiring at least 2 values inside {...}) of the sequence:
; - a semi-colon
(?<val>[^;]*) - Group "val" capturing 0+ chars other than ;
} - a literal }
\z - end of string.
.NET regex keeps each capture in a CaptureCollection stack, that is why all the values captured into "num" group can be accessed after a match is found.
C# demo:
var s = "{token1;token2;token3;...;tokenn}";
var pat = #"\A{(?<val>[^;]*)(?:;(?<val>[^;]*))+}\z";
var caps = new List<string>();
var result = Regex.Match(s, pat);
if (result.Success)
{
caps = result.Groups["val"].Captures.Cast<Capture>().Select(t=>t.Value).ToList();
}
Read it(similar to your problem): How to keep the delimiters of Regex.Split?.
For your RegEx testing use this: http://www.regexlib.com/RETester.aspx?AspxAutoDetectCookieSupport=1.
But RegEx is a very resource-intensive, slow operation.
In your case will be better to use the Split method of string class, for example : "token1;token2;token3;...;tokenn".Split(';');. It will return to you a collection of strings, that you want to obtain.
I'm trying to search a string for words within single quotes, but only if those single quotes are not within parentheses.
Example string:
something, 'foo', something ('bar')
So for the given example I'd like to match foo, but not bar.
After searching for regex examples I'm able to match within single quotes (see below code snippet), but am not sure how to exclude matches in the context previously described.
string line = "something, 'foo', something ('bar')";
Match name = Regex.Match(line, #"'([^']*)");
if (name.Success)
{
string matchedName = name.Groups[1].Value;
Console.WriteLine(matchedName);
}
I would recommend using lookahead instead (see it live) using:
(?<!\()'([^']*)'(?!\))
Or with C#:
string line = "something, 'foo', something ('bar')";
Match name = Regex.Match(line, #"(?<!\()'([^']*)'(?!\))");
if (name.Success)
{
Console.WriteLine(name.Groups[1].Value);
}
The easiest way to get what you need is to use an alternation group and match and capture what you need and only match what you do not need:
\([^()]*\)|'([^']*)'
See the regex demo
Details:
\( - a (
[^()]* - 0+ chars other than ( and )
\) - a )
| - or
' - a '
([^']*) - Group 1 capturing 0+ chars other than '
' - a single quote.
In C#, use .Groups[1].Value to get the values you need. See the online demo:
var str = "something, 'foo', something ('bar')";
var result = Regex.Matches(str, #"\([^()]*\)|'([^']*)'")
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToList();
Another alternative is the one mentioned by Thomas, but since it is .NET, you may use infinite-width lookbehind:
(?<!\([^()]*)'([^']*)'(?![^()]*\))
See this regex demo.
Details:
(?<!\([^()]*) - a negative lookbehind failing the match if there is ( followed with 0+ chars other than ( and ) up to
'([^']*)' - a quote, 0+ chars other than single quote captured into Group 1, and another single quote
(?![^()]*\)) - a negative lookahead that fails the match if there are 0+ chars other than ( and ) followed with ) right after the ' from the preceding subpattern.
Since you'd want to exclude ', the same code as above applies.
I need to check if a user input resembles a parameter or not. It comes as a string (not changeable) and has to look like the following examples:
p123[2] -> writable array index
r23[12] -> read only array index
p3[7].5 -> writable bit in word
r1263[13].24 -> read only bit in word
15 -> simple value
The user is allowed to input any of them and my function has to distinguish them in order to call the proper function.
An idea would be to check for characters in a specific order e.g. "p[]", "r[]", "p[]." etc.
But I am not sure how to archive that without checking each single character and using multiple cases...
Any other idea of how to make sure that the user input is correct is also welcomed.
If you just need to validate user input that should come in 1 of the 5 provided formants, use a regex check:
Regex.IsMatch(str, #"^(?:(?<p>[pr]\d+)(?:\[(?<idx>\d+)])?(?:\.(?<inword>\d+))?|(?<simpleval>\d+))$")
See the regex demo
Description:
^ - start of string
(?: - start of the alternation group
(?<p>[pr]\d+) - Group "p" capturing p or r and 1 or more digits after
(?:\[(?<idx>\d+)])? - an optional sequence of [, 1 or more digits (captured into Group "idx") and then ]
(?:\.(?<inword>\d+))? - an optional sequence of a literal ., then 1 or more digits captured into Group "inword"
| - or (then comes the second alternative)
(?<simpleval>\d+) - Group "simpleval" capturing 1 or more digits
) - end of the outer grouping
$ - end of string.
If the p or r can be any ASCII letters, use [a-zA-Z] instead of [pr].
C# demo:
var strs = new List<string> { "p123[2]","r23[12]","p3[7].5","r1263[13].24","15"};
var pattern = #"^(?:(?<p>[pr]\d+)(?:\[(?<idx>\d+)])?(?:\.(?<inword>\d+))?|(?<simpleval>\d+))$";
foreach (var s in strs)
Console.WriteLine("{0}: {1}", s, Regex.IsMatch(s, pattern));
You can check if the input match with a regex pattern :
1 ) Regex.IsMatch(input,#"^p\d+\[\d+\]$"); // match p123[2]
2 ) Regex.IsMatch(input,#"^r\d+\[\d+\]$"); // match r23[12]
3 ) Regex.IsMatch(input,#"^p\d+\[\d+\]\.\d+$"); // match p3[7].5
4 ) Regex.IsMatch(input,#"^r\d+\[\d+\]\.\d+$"); // match r1263[13].24
5 ) Regex.IsMatch(input,#"^\d+$") ;// match simple value
I've tried several regex combinations to figure out this, but some or the condition fails,
I have an input string, that could only contain a given set of defined characters
lets say A , B or C in it.
how do I match for something like this?
ABBBCCC -- isMatch True
AAASDFDCCC -- isMatch false
ps. I'm using C#
^[ABC]+$
Should be enough: that is using a Character class or Character Set.
The Anchors '^' and '$' would be there only to ensure the all String contains only those characters from start to end.
Regex.Match("ABACBA", "^[ABC]+$"); // => matches
Meaning: a Character Set will not guarantee the order of he characters matched.
Regex.Match("ABACBA", "^A+B+C+$"); // => false
Would guarantee the order
I think you are looking for this:
Match m = Regex.Match("abracadabra", "^[ABC]*$");
if (m.Success) {
// Macth
}