Get substrings from a string enclosed using a specific character

Suppose I have a string

Like (20)

I want to get a substring enclosed in parentheses (in the upper case there are 20) from this string. This substring can dynamically change at runtime. It can be any other number from 0 to infinity. For this, I would like to use a for loop that traverses the entire line, and then when present ( it starts adding characters to another character array and when it occurs ) , it stops adding characters and returns an array. But I think this may have poor performance. I don't know much about regular expressions, so is there a regex solution available or any function that can do this in an efficient way?

+4
source share
6 answers

If you do not want to use regex, you can use Split :

 string foo = "Likes (20)"; string[] arr = foo.Split(new char[]{ '(', ')' }, StringSplitOptions.None); string count = arr[1]; 

Count = 20

This will work fine regardless of the number in brackets ()

eg:

Likes (242535345)

It gives:

242535345

+5
source
 const string likes = "Likes (20)"; int likesCount = int.Parse(likes.Substring(likes.IndexOf('(') + 1, (likes.Length - likes.IndexOf(')') + 1 ))); 
+1
source

For strict compliance you can:

 Regex reg = new Regex(@"^Likes\((\d+)\)$"); Match m = reg.Match(yourstring); 

this way you will have everything you need in m.Groups[1].Value .

As suggested from I4V, assuming that you only have this sequence of digits in the entire string, as in your example, you can use a simpler version:

 var res = Regex.Match(str,@"\d+") 

and in this canse you can get the value you are looking for with res.Value

EDIT

If the value enclosed in brackets is not a number, you can just change \ d with something like [\w\d\s] if you want to allow alphabetic characters, numbers and spaces.

+1
source

It also works with clean string methods:

 string result = "Likes (20)"; int index = result.IndexOf('('); if (index >= 0) { result = result.Substring(index + 1); // take part behind ( index = result.IndexOf(')'); if (index >= 0) result = result.Remove(index); // remove part from ) } 

Demo

+1
source

Matching when the part in paranthesis must be a number;

 string inputstring="Likes (20)" Regex reg=new Regex(@"\((\d+)\)") string num= reg.Match(inputstring).Groups[1].Value 

Explanation: By definition, regexp corresponds to a substring, so unless you specify otherwise, the string you are looking for can occur anywhere in your string.

\ d stands for numbers. It will match any single digit.

We want this to potentially be repeated several times, and we want at least one. The + sign is a regular expression for a previous character or group that is repeated 1 or more times.

So, \ d + will match one or more digits. It will match 20.

To make sure that we get a number that is in paranteses, we say that it should be between (and). These are special characters in regexp, so we need to avoid them.

(\ d +) would correspond to (20), and we are almost there.

Since we want the part inside the brackets, and not including the brackets, we tell regexp that the part of the digits is a separate group.

We do this using brackets in our regular expression. ((\ d +)) will still match (20), but now it will be noted that 20 is a subgroup of this match, and we can get it from Match.Groups [].

For any line in parentheses, things get a little more complicated.

 Regex reg=new Regex(@"\((.+)\)") 

Will work for many lines. (dot matches any character). But if the input signal looks like β€œThis is an example (parantesis1) (parantesis2)”, you would have to match (parantesis1) (parantesis2) with parantesis1) (parantesis2 as a captured subgroup. Be what you need.

The solution may be to match for "any character other than the closing particle"

 Regex reg=new Regex(@"\(([^\(]+)\)") 

This will find (parantesis1) as the first match with parantesis1 as .Groups [1].

It will still fail for nested parenteza, but since regular expressions are not the right tool for nested parenteza, I feel this case is a bit out of scope.

If you know that a line always starts with "Likes" in front of the group, then it is better to save the solution.

+1
source

Even with Linq:

 var s = "Likes (20)"; var s1 = new string(s.SkipWhile(x => x != '(').Skip(1).TakeWhile(x => x != ')').ToArray()); 
+1
source

Source: https://habr.com/ru/post/1500677/


All Articles