How to extract all strings between brackets using c#? [duplicate] - c#

This question already has answers here:
How do I extract text that lies between parentheses (round brackets)?
(19 answers)
Closed 4 years ago.
If I have a string such as:
"You id is (1) and your number is (0000000000)"
What is the best way to extract these these strings into a list of strings. The numbers between the brackets can increase in digits thus searching for the strings between the brackets is a better technique.
I can use the code below to extract the first string between brackets.
var myString = "You id is (1) and your number is (0000000000)";
var firstNumberBetweenBrackets = myString.Split('(', ')')[1]; // would return 1

Here is a LINQ solution:
var result = myString.Split().Where(x => x.StartsWith("(") && x.EndsWith(")")).ToList();
Values stored in result:
result[0] = (1)
result[1] = (0000000000)
And if you want only the numbers without the brackets use:
var result = myString.Split().Where(x => x.StartsWith("(") && x.EndsWith(")"))
.Select(x=>x.Replace("(", string.Empty).Replace(")", string.Empty))
.ToList();
Values stored in result:
result[0] = 1
result[1] = 0000000000

You can use Regex for this (https://regex101.com/r/T4Sdik/1):
Regex regex = new Regex(#"\(([^()]+)\)*");
foreach (Match match in regex.Matches("You id is (1) and your number is (0000000000)")
{
Console.WriteLine(match.Value);
}
This will print:
1
0000000000

If you only want to get strings of digits inside parentheses, you may use
var results = Regex.Matches(s, #"\((\d+)\)")
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToList();
The results are in Group 1, so you need to access them with m.Groups[1].Value.
Regex details
\( - matches a literal (
(\d+) - Capturing group 1: one or more (due to the + quantifier) digits (you may use [0-9] instead of \d to only match ASCII digits, or use the RegexOptions.ECMAScript)
\) - a literal )
See the online C# demo and a regex demo here:

Related

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"]);
}

getting numbers from a string c# [duplicate]

This question already has answers here:
Using regex to extract multiple numbers from strings
(4 answers)
Closed 5 years ago.
I have an input string like below:
"/myWS/api/Application/IsCarAvailable/123456/2017"
It is the end of a Web API call that I am making. I need to easily extract the 123456 from the URL.
I was hoping something like the below would work
string[] numbers = Regex.Split(input, #"\D+");
However, when I set a breakpoint on numbers and run the code it is showing an array of 3 elements?
Element at [0] is ""
Element at [1] is 123456
Element at [2] is 2017
Does anyone see why it would be getting the empty string as the first element?
I suggest matching, not splitting:
string source = #"/myWS/api/Application/IsCarAvailable/123456/2017";
string[] numbers = Regex
.Matches(source, "[0-9]+")
.OfType<Match>()
.Select(match => match.Value)
.ToArray();
Please, notice that \d+ in .Net means any unicode digits (e.g. Persian ones: ۰۱۲۳۴۵۶۷۸۹): that's why I put [0-9]+ pattern (RegexOptions.ECMAScript is an alternative if \d+ pattern is preferrable).
If your string is always in the same format, this would work:
string numbers = input.Split('/')[input.Split('/').Length - 2];
I think this is because the split method "splits" the string at the matching expression. So the empty string is the part before the first match.
Any reason why you would not use Regex.Matches(input,"\\d+") instead?
string numtest = "http://www.google.com/test/123456/7890";
var matchResult = Regex.Matches(numtest, "\\d+");
for (int i = 0; i < matchResult.Count; i++)
Console.WriteLine($"Element {i} is {matchResult[i].Value}");
Hope that helps. Regards!

Regex Match multiple occurences with numbers in string C#

I've been searching for my problem answer, but couldn't find so I write here.
I want to take a string example: = "37513220102304920105590"
and find all matches for numbers of length 11 which starts 3 or 4.
I have been trying to do so:
string input = "37513220102304920105590"
var regex = new Regex("^[3-4][0-9]{10}$");
var matches = regex.Matches(trxPurpose);
// I expect it to have 3 occurances "37513220102", "32201023049" and "30492010559"
// But my matches are empty.
foreach (Match match in matches)
{
var number = match.Value;
// do stuff
}
My question is: Is my regex bad or I do something wrong with mathing?
Use capturing inside a positive lookahead, and you need to remove anchors, too. Note the - between 3 and 4 is redundant.
(?=([34][0-9]{10}))
See the regex demo.
In C#, since the values are captured, you need to collect .Groups[1].Value contents, see C# code:
var s = "37513220102304920105590";
var result = Regex.Matches(s, #"(?=([34][0-9]{10}))")
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToList();

regex to split numbers and operators

How do I split below string to list of string with numbers and operators separated (string does not contain parenthesis or negative numbers).
Example:
inputString = 1+2-2.3*4/12.12
outputList = {1,+,2,-,2.3,*,4,/,12.12}
Below will give me numbers only. I need operators as well
var digits = Regex.Split(inputString , #"\D+");
Since you confirm the structure of the input is rather simplistic - no parentheses, no negative numbers - you can just use a simple \s*([-+/*])\s* regex to split the string.
Note that Regex.Split will also output all captured substrings in the result:
If capturing parentheses are used in a Regex.Split expression, any captured text is included in the resulting string array.
So, use
Regex.Split(input, #"\s*([-+/*])\s*")
.Where(n => !string.IsNullOrEmpty(n))
.ToList();
Just do not forget to remove empty elements from the resulting list/array.
Pattern details:
\s* - zero or more whitespaces (to "trim" the elements)
([-+/*]) - Group 1 capturing a -, +, / or *
\s* - zero or more whitespaces (to "trim" the elements)
See the IDEONE demo:
var input = "1+2-2.3*4/12.12";
var results = Regex.Split(input, #"\s*([-+/*])\s*")
.Where(n => !string.IsNullOrEmpty(n))
.ToList();
Console.WriteLine(string.Join("\n", results));
You could use Regex.Matches instead of Regex.Split :
var test = "1 + 2 - 2.3 * 4 / 12.12";
foreach(Match match in Regex.Matches(test, #"\d+(,\d+)*(\.\d+(e\d+)?)|\d+|[\\+-\\*]"))
Console.WriteLine(match.Value);
This seemed to work for me
/([\d\.]+)|([+-\/\*])+/g
FYI - LinqPad is an awesome tool to test Regex in C#

Regex to remove all (non numeric OR period)

I need for text like "joe ($3,004.50)" to be filtered down to 3004.50 but am terrible at regex and can't find a suitable solution. So only numbers and periods should stay - everything else filtered. I use C# and VS.net 2008 framework 3.5
This should do it:
string s = "joe ($3,004.50)";
s = Regex.Replace(s, "[^0-9.]", "");
The regex is:
[^0-9.]
You can cache the regex:
Regex not_num_period = new Regex("[^0-9.]")
then use:
string result = not_num_period.Replace("joe ($3,004.50)", "");
However, you should keep in mind that some cultures have different conventions for writing monetary amounts, such as: 3.004,50.
You are dealing with a string - string is an IEumerable<char>, so you can use LINQ:
var input = "joe ($3,004.50)";
var result = String.Join("", input.Where(c => Char.IsDigit(c) || c == '.'));
Console.WriteLine(result); // 3004.50
For the accepted answer, MatthewGunn raises a valid point in that all digits, commas, and periods in the entire string will be condensed together. This will avoid that:
string s = "joe.smith ($3,004.50)";
Regex r = new Regex(#"(?:^|[^w.,])(\d[\d,.]+)(?=\W|$)/)");
Match m = r.match(s);
string v = null;
if (m.Success) {
v = m.Groups[1].Value;
v = Regex.Replace(v, ",", "");
}
The approach of removing offending characters is potentially problematic. What if there's another . in the string somewhere? It won't be removed, though it should!
Removing non-digits or periods, the string joe.smith ($3,004.50) would transform into the unparseable .3004.50.
Imho, it is better to match a specific pattern, and extract it using a group. Something simple would be to find all contiguous commas, digits, and periods with regexp:
[\d,\.]+
Sample test run:
Pattern understood as:
[\d,\.]+
Enter string to check if matches pattern
> a2.3 fjdfadfj34 34j3424 2,300 adsfa
Group 0 match: "2.3"
Group 0 match: "34"
Group 0 match: "34"
Group 0 match: "3424"
Group 0 match: "2,300"
Then for each match, remove all commas and send that to the parser. To handle case of something like 12.323.344, you could do another check to see that a matching substring has at most one ..

Categories

Resources