Solved

Regular expression question

Posted on 2009-05-07
3
295 Views
Last Modified: 2012-05-06
I have a string with the following format:

(some_text_1)
cedhjbd"^*&ljhbdc_ckjnbc`
(some_text_2)

How can i leave only the text within the brackets. I tried a few things like replacing anything between ) and ( with a \n but this didn't seem to work as there can be any number of lines/symbols etc between the sections in brackets.

Cheers.
0
Comment
Question by:mike_pro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Expert Comment

by:RyanAndres
ID: 24329146
Try this out.
// STATEMENTS TO CALL SOMEWHERE
string str = "(some_text_1)\ncedhjbd\"^*&ljhbdc_ckjnbc`\n(some_text_2)";
string result = GetInnerText(str);
 
// METHOD TO DO EXTRACTION
public string GetInnerText(string input)
{
    StringBuilder result = new StringBuilder();
    Regex regex = new Regex(@"\((.*)\)");
    MatchCollection matches = regex.Matches(input);
 
    foreach (Match match in matches)
    {
        if (match.Groups.Count > 0)
        {
            string innerText = match.Groups[1].Value;
            result.AppendLine(innerText);
        }
    }
 
    return result.ToString();
}

Open in new window

0
 
LVL 8

Accepted Solution

by:
RyanAndres earned 500 total points
ID: 24329159
The above code will return:
some_text_1
some_text_2

or:
"some_text_1\r\nsome_text_2\r\n"
0
 

Author Closing Comment

by:mike_pro
ID: 31579147
Cheers! That was exactly what i was after.
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question