Solved

Regular expression question

Posted on 2009-05-07
3
284 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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 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