Get the substring of the non conditional part

Get the substring of the non conditional part



I have this string for example:
2X+4+(2+2X+4X) +4

The position of the parenthesis can vary. I want to find out how can I extract the part without the parenthesis. For example I want 2X+4+4. Any Suggestions?
I am using C#.


2X+4+(2+2X+4X) +4


2X+4+4






Possible duplicate: stackoverflow.com/questions/17779744/…

– Aldert
Sep 14 '18 at 5:15






@Aldert This is a C# question how can you make it a duplicate of a javascript question? Please remove your flags

– Zeeshan Adil
Sep 14 '18 at 5:19






@Shahrier, this is because I am looking for a good solution for you and this would be regex, especially when you have parenthisis in perenthises. I did not even look at the language.

– Aldert
Sep 14 '18 at 5:22







If you just remove the parentheses you get 2X+4++4. Am I correct that you want only 1 plus between the two 4's? What if it's not plusses but other operators?

– Hans Kilian
Sep 14 '18 at 5:25


2X+4++4






What would happen if you had 2X+4+(2+2X+(3X+7X)+4X)+4? Is that possible to get?

– Enigmativity
Sep 14 '18 at 5:56



2X+4+(2+2X+(3X+7X)+4X)+4




3 Answers
3



Try this one:


var str = "(7X+2)+2X+4+(2+2X+(3X+3)+4X)+4+(3X+3)";

var result =
str
.Aggregate(
new Result = "", depth = 0 ,
(a, x) =>
new

Result = a.depth == 0 && x != '(' ? a.Result + x : a.Result,
depth = a.depth + (x == '(' ? 1 : (x == ')' ? -1 : 0))
)
.Result
.Trim('+')
.Replace("++", "+");

//result == "2X+4+4"



This handles nested, preceding, and trailing parenthesis.



Try Regex approach:


var str = "(1x+2)-2X+4+(2+2X+4X)+4+(3X+3)";
var regex = new Regex(@"(S+?)W?");//matches '(1x+2)-', '(2+2X+4X)+', '(3X+3)'
var result = regex.Replace(str, "");//replaces parts above by blank strings: '2X+4+4+'
result = new Regex(@"W$").Replace(result, "");//replaces last operation '2X+4+4+', if needed
//2X+4+4 ^






Can you please share why this Regex work? or maybe share some link to understand the Regex patterns? Thanks

– Zeeshan Adil
Sep 14 '18 at 5:29






This would fail on "2X+4+(2+2X+4X)+4+(3X+3)".

– Enigmativity
Sep 14 '18 at 5:58


"2X+4+(2+2X+4X)+4+(3X+3)"






I agree, this approach using Regex is much more neat

– Mrinal Kamboj
Sep 14 '18 at 5:58






@Enigmativity, your case is fixed

– Slava Utesinov
Sep 14 '18 at 6:11



Try simple string Index and Substring operations as follows:


string s = "2X+4+(2+2X+4X)+4";

int beginIndex = s.IndexOf("(");
int endIndex = s.IndexOf(")");

string firstPart = s.Substring(0,beginIndex-1);
string secondPart = s.Substring(endIndex+1,s.Length-endIndex-1);

var result = firstPart + secondPart;


(


)


beginIndex


endIndex



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)