473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Regex guru help please...

Hi,

We have a bunch of strings in the following format:

[xxxxx] yyyy [xxxxx] yyyyy

We would like to keep all of the "xxxxx" data - anything that falls within
the square brackets. We know that Regex can do this but, whooaa - how do you
make it do this?
Nov 17 '05 #1
3 1108
PW
"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
Hi,

We have a bunch of strings in the following format:

[xxxxx] yyyy [xxxxx] yyyyy

We would like to keep all of the "xxxxx" data - anything that falls within
the square brackets. We know that Regex can do this but, whooaa - how do
you
make it do this?


Regex regex = new Regex(@"\[([^\]]+)\]");
Match m = regex.Match(YOUR_TEXT);
StringCollection stringCollection = new StringCollection();

while (m.Success)
{
stringCollection.Add(m.Groups[1].Captures[0].Value);
m = m.NextMatch();
}
Nov 17 '05 #2
Thanx alot! Is there any way I can have it not return the square braces in
the result?

"PW" wrote:
"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
Hi,

We have a bunch of strings in the following format:

[xxxxx] yyyy [xxxxx] yyyyy

We would like to keep all of the "xxxxx" data - anything that falls within
the square brackets. We know that Regex can do this but, whooaa - how do
you
make it do this?


Regex regex = new Regex(@"\[([^\]]+)\]");
Match m = regex.Match(YOUR_TEXT);
StringCollection stringCollection = new StringCollection();

while (m.Success)
{
stringCollection.Add(m.Groups[1].Captures[0].Value);
m = m.NextMatch();
}

Nov 17 '05 #3
Richard wrote:
We have a bunch of strings in the following format:

[xxxxx] yyyy [xxxxx] yyyyy

We would like to keep all of the "xxxxx" data - anything that falls within
the square brackets. We know that Regex can do this but, whooaa - how do
you
make it do this?


In addition to PW's suggestion, you can also get the modified string back
from a regex replacement in one go. Just call Regex.Replace to replace this:

(?<!\[[^\]]*)[^\[\]]*(?![^\[]*\])

by nothing (just pass in an empty string for the replacement parameter. It
should return

[xxxxx][xxxxx]

for your sample above.

If you want to get rid of the brackets themselves as well, use this
expression instead:

\]?(?<!\[[^\]]*)[^\[\]]*(?![^\[]*\])\[?

This will give the result

xxxxxxxxxx

for your sample.

Looks a bit like line noise, doesn't it? :-)
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: chris | last post by:
i can see the power of regular expressions but am having a bit of a battle getting my head around them. can anyone recommend some BASIC - tutorials for using regex something like th idots...
9
by: William Ryan | last post by:
How do I ignore something that's part of what I want to find. For instance..I have this string I have a regex \]\d*\] to find the whole thing. However, I don't want to have either of the...
3
by: Day Of The Eagle | last post by:
Jeff_Relf wrote: > ...yet you don't even know what RegEx is. > I'm looking at the source code for mono's Regex implementation right now. You can download that source here ( use the class...
2
by: Julie | last post by:
I have to process parameter input strings of key/value pairs /key=value where value _may_ contain a slash /. Here is a sample input string: /def=gamma chain (FT2) /name=NULL /p_id=CF11423:SF7...
3
by: Richard | last post by:
Trying to wrap my brain around Regex for the first time... I need to be able to replace Sql @NamedParameters with "?" for compliance with an extremely lame 3rd party Odbc driver. I know that...
1
by: Indian Offshore Company | last post by:
Hi, I want to parse html with multiple <a href.. text...</atags as follow: "... some html... <a class="1" href="city1.html" onclick="etc."click for info on city1 </a.. some html.. <a...
5
by: Bragadiru | last post by:
Hi, I'm using the following Regex to parse for method parameters. It works if there are no spaces between commas. How can I change the regex to support method calls like : MyMethod('uno', ...
7
by: shaolinman829 | last post by:
I suck at regex's and I need a little hope. Hopefully there is a guru out there to help me on this one. I want to do a match on the following: one or more numeric characters zero or more...
16
by: Singulus | last post by:
Hello all, I've searched for similar threads, I've found some bit of useful info here and there, but nevertheless I want to post my questions...So, how can I (we, in fact the forum can benefit...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.