JITU99
(jitendra)
May 23, 2023, 12:13pm
1
Hi I have a sentence as follows.
I should retrieve the substring whic is outside the circular brackets.
Can someone help please?
Exampl 1: (Roses) are (Red Milk is) white
should return-> are and white seperately in an array.
arr[0] = are
arr[1] = white
Example 2:roses) are red milk (is white)
result
arr[0] = are red milk
ushu
(Usha kiranmai)
May 23, 2023, 12:20pm
2
Hi @JITU99 - Try the below exp
(?<=\(.*\)).*(?=\(.*\))|(?<=\(.*\).*\(.*\)).*
ppr
(Peter Preuss)
May 23, 2023, 12:29pm
3
Assign Activity
strPattern = (?<=\)).*?(?=\(|$)
Assign Activity
arrValues | String Array =
System.Text.RegularExpressions.RegEx.Matches(YourStringVar, strPattern).Cast(Of Match).Select(Function (x) x.Value.Trim).toArray
First Value → arrValues(0)
Second Value → arrValues(1)
JITU99
(jitendra)
May 23, 2023, 1:13pm
4
Thanks @ppr for the solution but for some cases its not working examples below.With outputs
(successful) football (club(s)) (managers)" ----> {“football”,“)”,“”}
“boat(s)” ----> empty array
“abcd (asdf) boat(s)” ----> {“boat”,“”}
ppr
(Peter Preuss)
May 23, 2023, 2:09pm
5
better that you will write down all variations (input, output), so we can check for a new pattern
For regex is not recommended to start with half-defined requirements and then run it against another case. Regex is not about that a pattern can support every case. Also the strategy to setup an extraction over a regex extraction cascade / regex / string methods cascade can help as well
JITU99
(jitendra)
May 23, 2023, 2:18pm
6
Hi @ppr Most of my combinations are these but there can be chances the sentence is more complex or bigger one with the brackets.
Are these combinations enough?
(successful) football (club(s)) (managers) volleyball Cricket (New(s))
(a) farmer (s)
(the) farmer (s)
(a) teacher (s)
(the) teacher (s)
(successful) football (club(s)) (managers)
(successful) soccer (club(s)) (managers)
(a) (new) magazine (s) (for teenagers)
(the) (new) magazine (s) (for teenagers)
(a) boat (s)
(the) boat (s)
Tuesday (s)
abcd (asdf) boat (s)
boat (s)
(successful) football (club(s)) (managers)
(successful) football clubs (managers)
Abcd (efgh) hgji asdf (xyz) qwerty ------> hgji asdf should be one out put instead of hdji and asdf being sepearetly in the array of string
ppr
(Peter Preuss)
May 23, 2023, 2:22pm
7
can you just mark in bold / or list down what output is needed? Thanks
JITU99
(jitendra)
May 23, 2023, 2:32pm
8
i just edited my question
ppr
(Peter Preuss)
May 23, 2023, 2:44pm
9
Maybe a replacement strategy is better fitting
We replace the brackets with a #
and then split / extract / trim the remaining parts
JITU99
(jitendra)
May 23, 2023, 2:50pm
10
That will work but can i get the query for it please?
ppr
(Peter Preuss)
May 23, 2023, 3:31pm
11
strText
"(successful) football (club(s)) (managers) volleyball Cricket (New(s))"
strPattern
"\(.+?\)?\)"
System.Text.RegularExpressions.Regex.Replace(strText,strPattern,"#").Split({"#"}, StringSplitOptions.RemoveEmptyEntries)
string[3] { " football ", " ", " volleyball Cricket " }
arrValues = System.Text.RegularExpressions.Regex.Replace(strText,strPattern,"#").Split({"#"}, StringSplitOptions.RemoveEmptyEntries)
string[3] { " football ", " ", " volleyball Cricket " }
arrValues = arrValues.Select(Function (x) x.Trim() ).Where(Function (x) Not String.IsNullOrEmpty(x)).toArray
string[2] { "football", "volleyball Cricket" }
system
(system)
Closed
May 27, 2023, 4:51am
13
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.