Performing OR in regex capture groups [duplicate] - c#

This question already has answers here:
Using alternation and grouping constructs
(2 answers)
Closed 4 years ago.
I would like a match if the value contains either digits or matches the pattern [lookup('key')] where key can be any string.
Using either pattern on its own works. For example this works.
string value = "[lookup('anykey')]";
if (!Regex.IsMatch(value, "^\\[(lookup)\\(.*\\)\\]$"))
{
Console.WriteLine("no match");
}
I couldn't get both to work with a single regex.
if (!Regex.IsMatch(value, "((\\d+) | (\\[(parameter)\\(.*\\)\\]))"))
{
Console.WriteLine("no match");
}
Any idea?

In your regex you should remove spaces, try:
\d+|\[lookup\('[^']+'\)\]

First of all whenever I use regular expression in c# or for that matter any string that has characters that require escaping I prefix the string with '#' symbol, which saves me from using double escapes:)...I find it easier
Ok now to the answer, here is what I think you are looking for
static void Main(string[] args)
{
//string value = "[lookup('BlahBlah')]";
string value = "789897";
Match m = Regex.Match(value, #"((\d+)|(\[lookup\(\'([^\']+)\'\)\]))") ;
if (m.Success)
{
string num = m.Groups[2].Value;
string key = m.Groups[4].Value;
}
}
Notice how the string was prefixed with the '#' symbol and I didn't have to use double escapes for symbol \.
Since we are using so many parenthesis, we have 5 groups overall and if you want the number then you take the value of group 1, if you want the key the you take the value of group 4. If the 'num' is an empty string that means no number was supplied etc...
Hope that helps .....

Related

How to find the immediate integer value written before a string? [duplicate]

This question already has answers here:
How to get the digits before some particular word using regex in c#?
(8 answers)
Closed 2 years ago.
How to find the immediate integer value written before a string in c#? For example
50+ boxes were ordered, however only 2 are delivered.
I need to know the number of boxes (integer value) written just before "delivered". The output should be 2. I have written a code in c# using Regex:
string line = "50+ boxes were ordered, however only 2 are delivered.";
string boxesDelivered = Regex.Match(line, #"\d+").Value;
//The output I get is 50 instead of 2.
To get the last number that is followed by the word "delivered", you may use the following pattern:
\b\d+\b(?=[^\d]*\bdelivered\b)
Regex demo.
Here's a full example:
string line = "50+ boxes were ordered, however only 2 are delivered.";
var match = Regex.Match(line, #"\b\d+\b(?=[^\d]*\bdelivered\b)");
if (match.Success)
{
string boxesDelivered = match.Value;
// TODO: convert the value to a numeric type or use it as is.
}
Try it online.
written just before delivered
I'm going to take that verbatim as your user requirement - find the last number in the string that appears before "delivered".
You can use (\d+)[^\d]*(?:delivered), which says "match any sequence of numbers that does not occur before another sequence of numbers and does occur before delivered".
string line = "50+ boxes were ordered, however only 2 are delivered.";
string boxesDelivered = Regex.Match(line, #"(\d+)[^\d]*(?:delivered)").Groups[1].Value;
// boxesDelivered = 2

Regex to get string between number and underscore C# [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
i'm tryng make a regex to get the string between some number and underscore, for example:
I have CP_01Ags_v5, so I need a regex to match just Ags. another example could be CP_13Hgo_v5 and match Hgo.
Some idea?
Based off the examples and matches you are describing. You want something along the lines of.
[0-9]+(.*)[_]
to break it down.
The regex looking for any number that shows up one or more times then matches everything after the number(s) up until the [_] underscore.
The downfall is this assumes the examples you provided are similar. If your example is
CP_13Hgo_v5asdf_
then it will match
Hgo_v5asdf
if you have other possible findings then you want the non-greedy version of this regex.
[0-9]+(.*?)[_]
this will cause two groups to be found in this example
CP_13Hgo_v5asdf_
will find the following groups:
Hgo
and
asdf
You can use look-arounds to match just the string between the digits and the underscore e.g.
(?<=\d)[A-Za-z]+(?=_)
Demo on regex101
In C# (note the need to escape the \ in the regex):
String s = #"CP_01Ags_v5 CP_13Hgo_v5";
Match m = Regex.Match(s, "(?<=\\d)[A-Za-z]+(?=_)");
while (m.Success) {
Console.WriteLine(m.Value);
m = m.NextMatch();
}
Output
Ags
Hgo
If your string is always at least two characters and there are no other strings of at least two characters, then you can apply the following:
var text = "CP_01Ags_v5";
var x = Regex.Match(text, #"(?<!^)[A-Za-z]{2,}");
Use Regex Group:
(?<leftPart>_\d{2})(?<YourTarget>[a-zA-Z])(?<rightPart>_[a-zA-Z0-9]{2})
C#:
Regex re = new Regex(#"(?<leftPart>_\d{2})(?<YourTarget>[a-zA-Z])(?<rightPart>_[a-zA-Z0-9]{2})");
/*
* Loop
* To get value of group you want
*/
foreach (Match item in re.Matches("CP_01Ags_v5 CP_13Hgo_v5,"))
{
Console.WriteLine(" Match: " + item.ToString());
Console.WriteLine(" Your Target you want: " + item.Groups["YourTarget"]);
}

Splitting a string in C# to extract values in parentheses and keep them [duplicate]

This question already has answers here:
Regular expression to extract text between square brackets
(15 answers)
Closed 3 years ago.
I need to split a string to extract the parentheses and data in a string array using a Regex and keep the parentheses as well.
Extract from
1-2-3(0)(1)
To
(0)
(1)
I constructed this Regex, but can't make it work.
String phrase= "123(0)(1)"
String[] results = Regex.Split(phrase,"\\r+(?:\\(.*\\))");
You can use Regex.Matches method instead
string phrase = "123(0)(1)";
string[] results = Regex.Matches(phrase, #"\(.*?\)").Cast<Match>().Select(m => m.Value).ToArray();
You could try using substring method if those two in parenthesis will always be together.
phrase = phrase.Substring(phrase.FirstIndexOf("("));
Might have to put -1 after.
You can extract the numbers in parentheses using (\(\d\)) pattern.
https://regex101.com/r/chjyLN/1
E.g.
var input = "1-2-3(0)(1)";
Regex pattern = new Regex(#"(\(\d\))");
var matches = pattern.Matches(input);
foreach (Match match in matches)
{
foreach (Capture capture in match.Captures)
{
Console.WriteLine(capture.Value);
}
}

Regex expression -string between first and last single quotes [duplicate]

This question already has answers here:
C# Regular Expressions, string between single quotes
(4 answers)
Closed 5 years ago.
I have some strings. I want total string between first and last single quote
for example:
string val = "'Scale['#13212']'"; //--->Scale['#13212']
string val2= "'Scale[#13212']"; //--->Scale[#13212
string val3="abc'23'asad"; //--->23
I have use the following regex-#".*'(.*?)'.*" but it only displays string between last two.
for example:
string val = "'Scale['#13212']'"; //--->]
It is working fine with greedy when I use to capture the whole value of a string and a group(in group[1] ONLY) enclose with a pair of single quote
But when I want to capture the whole value of a string and a group(in group[1] ONLY) enclose with multiple pair of single quote , it only capture the value of string enclose with last pair but not the string between first and last single quotes.
for example:
string val1 = "Content:abc'23'asad"; //--->23
string val2 = "Content:'Scale['#13212']'ta";
Match match1 = Regex.Match(val1, #".*'(.*)'.*");
Match match2 = Regex.Match(val2, #".*'(.*)'.*");
if (match1.Success)
{
string value1 = match1.Value;
string GroupValue1 = match1.Groups[1].Value;
Console.WriteLine(value1);
Console.WriteLine(GroupValue1);
string value2 = match2.Value;
string GroupValue2 = match2.Groups[1].Value;
Console.WriteLine(value2);
Console.WriteLine(GroupValue2);
Console.ReadLine();
// using greedy For val1 i am getting perfect value for-
// value1--->Content:abc'23'asad
// GroupValue1--->23
// BUT using greedy For val2 i am getting the string elcosed by last single quote-
// value2--->Content:'Scale['#13212']'ta
// GroupValue2---> ]
// But i want GroupValue2--->Scale['#13212']
}
Please help!
Just use greedy .* between quotes:
'(.*)'
Demo
You can use Positive Lookbehind and Positive Lookahead to take the text between the quotes (Demo)
(?<=\').*(?=\')
Try below
string test= "'$get this$' test value";
string getvalue = Regex.Match(test, #"\'(.*?)\'").Groups[1].ToString();

In Regex, how do to add one extra space between every word starting with Capital [duplicate]

This question already has answers here:
Add spaces before Capital Letters
(32 answers)
Closed 9 years ago.
I have a string: CategoryName
I need to add space between Category and Name.
So I have to insert a space before each capital letter.
var input = "CategoryName";
var result = Regex.Replace(input, "([a-z])([A-Z])", #"$1 $2"); //Category Name
UPDATE (this will treat sequence of capital letters as one word)
var input = "SimpleHTTPRequest";
var result = Regex.Replace(input, "([a-z]|[A-Z]{2,})([A-Z])", #"$1 $2");
//Simple HTTP Request
This code will do the job
var source = "CategoryName";
var nameConvert = new Regex(#"((?<=[a-z])[A-Z]|(?<!^|\s)[A-Z][a-z])");
var converted = nameConvert.Replace(source, " $1");
This will leave multiple capital letters together e.g. FearTheCIAReally becomes Fear The CIA Really
To explain the regex:
( start capture group $1
(?<=[a-z])[A-Z] capital letter preceded by a lower case letter (don't capture lower case)
| or
(?<!^|\s) preceding character not space or start of string, but don't capture
[A-Z] capital letter
[a-z] followed by a lower case letter
) end capture group 1
I actually have this as a library function I use all the time
public static class StringExtensions {
private static readonly Regex NameConvert =
new Regex(#"((?<=[a-z])[A-Z]|(?<!^|\s)[A-Z][a-z])");
public static string ToDisplayFormat(this string name) {
return string.IsNullOrEmpty(name) ?
String.Empty :
NameConvert.Replace(name," $1");
}
}
And then I can just use it in code
var name="CategoryName";
var displayName = name.ToDisplayFormat();
([A-Z?])[_ ]?([a-z])
Try this Regular expression.
Hope it helps.

Categories

Resources