Solved

Regular expression question

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

9 Experts available now in Live!

Get 1:1 Help Now