I have the following code:
var varFormula = "IIF(ABCCF012HIZ3000=0,0,ABCCF012HCZ3000/ABCCF012HIZ3000)"
MatchCollection match = Regex.Matches(varFormula, #"^AB([CC|DD|EE]+[F|G])[0-9]{3}(HI|IC|HC)Z[A-Z0-9]{4}$", RegexOptions.IgnoreCase);
From above, I want to extract the following from varFormula but I'm not able to get any matches/groups:
ABCCF012HCZ3000
ABCCF012HIZ3000
Your regex uses ^ and $ which designate the start and end of a line. So it will never match the varFormula. Try the following:
var varFormula = #"IIF(ABCCF012HIZ3000=0,0,ABCCF012HCZ3000/ABCCF012HIZ3000)";
MatchCollection match = Regex.Matches(varFormula, #"AB(?:[CC|DD|EE]+[F|G])[0-9]{3}(?:HI|IC|HC)Z[A-Z0-9]{4}", RegexOptions.IgnoreCase)
Should give you three matches:
Match 1 ABCCF012HIZ3000
Match 2 ABCCF012HCZ3000
Match 3 ABCCF012HIZ3000
Related
I have a universal regex code where it uses Groups[1] value to extract the result. It's easy to extract SN and Ref by just giving a sn=(.*?)\. pattern. But it's so difficult to get for example, PKSC and V928. I have to use Groups[1] because users who use this application can choose their own value to display. It can be NC339 or PKXC.
//var source = "SN=1395939213.#variable/OGT84/PKXC/Undetermined.Thank You#{customer}"
//sometimes like this
var source = "SN=8029758034.Ref=BFO7Y95B3KN5#resolved/NC339/V928/ClearenceBBF.Brief#{supervisor}/verified"
var value = Regex.Match(source, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value
You can use
^(?:[^/]*/){2}([^/]+)
See the regex demo.
Details
^ - start of a string
(?:[^/]*/){2} - two occurrences of any chars other than / and then a /
([^/]+) - Group 1: one or more chars other than /.
Try following :
string pattern = #"SN=(?'sn'\d+).*\{(?'supervisor'[^}]+)";
string input = "SN=1395939213.#variable/OGT84/PKXC/Undetermined.Thank You#{customer}";
Match match = Regex.Match(input, pattern);
string sn = match.Groups["sn"].Value;
string supervisor = match.Groups["supervisor"].Value;
There doesn't seem to be a way to specify both RegexOptions and a start index when using Regex.Matches.
According to the docs, there is a way to do both individually, but not together.
In the example below, I want matches to contain only the second hEllo in the string text
string pattern = #"\bhello\b";
string text = "hello world. hEllo";
Regex r = new Regex(pattern);
MatchCollection matches;
// matches nothing
matches = r.Matches(text, 5)
// matches the first occurence
matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase)
Is there a different way to accomplish this?
I don't believe you can. You should instead instantiate Regex using the desired options:
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
and then you can simply use your existing code from the first sample, which should now match since we're using the IgnoreCase option:
matches = r.Matches(text, 5);
Applicable constructor docs
Try it online
We need to search the word "test.property" and replace "test1.field" in a single or multiple line.
word boundary won't ignore the \r\n, it can find the:
test.\r\nproperty
how to ignore the \r\n between words in regex C#?
ex:
Input sources:
int c = test\r\n.Property\r\n["Test"];
needed output :
int c = test1\r\n.Field\r\n["Test"];
My current output :
int c =test1.Field.["test"]
Pattern :
regex I'm using :
Regex regex = new Regex(#"\btest\s*\.\s*property\b", RegexOptions.IgnoreCase | RegexOptions.Singleline);
replacementLine = regex.Replace(sourceLine, "test1.field");
we need to replace only the string not the line break. Please give your suggestions?
try this :
Regex regex = new Regex(#"(?'g1'\btest\b\.\W*)(?'g3'\bproperty\b)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
var replacementLine = regex.Replace(sourceLine, "${g1}Field");
You need a lookbehind which also accepts the possible whitespace. Here is an example which ignores the first, changes both the second and third findings.
var data = #"testNO.property['LeaveThis'];
test
.property
['Test'];
test.property['Test2'];";
var pattern = #"(?<=test[\r\n\s]*\.)property";
Regex.Replace(data, pattern, "field")
Result of Replace
testNO.property['LeaveThis'];
test
.field
['Test'];
test.field['Test2'];
I would like to know how to extract complete words using a Regex in C#
For example, my String input:
This$#23 is-kt jkdls
I want to get Regex Match as
This$#23
is-kt
jkdls
I need to extract non space words [which can have numbers or special characters]
by specifying Regex Match pattern
Regex myrex = new Regex("pattern")
MatchCollection matches = Regex.Matches("This$#23 is-kt jkdls", #"\S+");
foreach(Match match in matches)
Console.WriteLine(match.Value);
Use \S+ to match words.
var words = string.Split(' ');
Howzit,
I need help with the following please.
I need to find tags in a string. These tags start with {{ and end with }}, there will be multiple tags in the string I receive.
So far I have this, but it doesn't find any matches, what am I missing here?
List<string> list = new List<string>();
string pattern = "{{*}}";
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
Match m = r.Match(text);
while (m.Success)
{
list.Add(m.Groups[0].Value);
m = m.NextMatch();
}
return list;
even tried string pattern = "{{[A-Za-z0-9]}}";
thanx
PS. I know close to nothing about regex.
Not only do you want to use {{.+?}} as your regex, you also need to pass RegexOptions.SingleLine. That will treat your entire string as a single line and the . will match \n (which it normally will not do).
Try {{.+}}. The .+ means there has to be at least one character as part of the tag.
EDIT:
To capture the string containing your tags you can do {{(.+)}} and then tokenize your match with the Tokenize or Scanner class?
I would recommend trying something like the following:
List<string> list = new List<string>();
string pattern = "{{(.*?)}}";
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
Match m = r.Match(text);
while (m.Success)
{
list.Add(m.Groups[1].Value);
m = m.NextMatch();
}
return list;
the regex specifies:
{{ # match {{ literally
( # begin capturing into group #1
.*? # match any characters, from zero to infinite, but be lazy*
) # end capturing group
}} # match }} literally
"lazy" means to attempt to continue matching the pattern afterwards "}}" before backtracking to the .*? and reluctantly adding a character to the capturing group only if the character does not match }} - hope that made sense.
I changed your code by modifying the regex and to extract the first matching group from the regex match object (m.Groups[1].value) instead of the entire match.
{{.*?}} or
{{.+?}}
. - means any symbol
? - means lazy(don't capute nextpattern)