Solved

Regular expression question

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Round up to 100% in .NET 10 79
C# Error - Add Failed 12 77
Iterate a dictionnary to change values 4 53
Filename to be appended with DateTimeof Download 3 40
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

937 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now