Solved

Regular expression question

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Name Space error VS2015 1 48
Run tasks synchronously in background thread 19 82
Stop Git from being my repository 1 51
.Net remove carriage returns, line feeds and tabbs 5 37
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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