473,394 Members | 1,699 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,394 software developers and data experts.

Regex Help?

This is a bit of an abuse of this group. Just a nit, but I'm hoping someone
really good with Regular Expressions can help me out here.

I need to write a regular expression that will do the following:

Search a whole blob of text (including newlines and everything).
Find any text enclosed in brackets ([xyz]), and replace it with a string I
provide and then concatenate the text that had been enclosed in the brakets,
without the brakets.

So, for example:
Input String:
"Hi there. My name is [Alex]. My brother's name is [Billy]."
Prepend Parameter:
"Mr. "

For this example, the output would be:
"Hi there. My name is Mr. Alex. My brother's name is Mr. Billy."

I'm sure that this is very simple for people who know RegEx's. I ain't one
of those people. Anyone's help would be MOST appreciated.

Alex
Aug 2 '06 #1
2 2350
The Regular Expression is simple:

\[([^][]+)\]

This says that a match consists of the '[' character followed by 1 or more
characters that are neither '[' nor ']' and that is followed by a ']'
character.

It also puts the actual contents of the brackets into a group, Group 1, as
in the following example:

Hi there. My name is [Alex]. My brother's name is [Billy].

In this example, "[Alex]" is a match, and so is "[Billy]". In the "Alex"
match, "Alex" is Group 1. In the "Billy" match, "Billy" is Group 1.

I would use the Regex.Replace overload which takes a string and a
MatchEvaluator delegate. A MatchEvaluator delegate is a method that takes a
System.Text.RegularExpressions.Match object as an argument, and returns the
replacement string for that Match. The Replace method replaces each Match
with the MatchEvaluator, and thus replaces the entire string. It is the
method that does the actual replacing. Example:

// MatchEvaluator delegate
private string bracketReplacer(Match m)
{
// strips the brackets and returns the value enclosed in them.
return m.Groups[1].Value;
}

// Replacement Function
public string ReplaceBrackets(string s)
{
Regex regex = new Regex(@"\[([^][]+)\]");
return regex.Replace(s, new MatchEvaluator(bracketReplacer));
}

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Who is Mighty Abbott? A twin-turret scalawag.

"Alex Maghen" <Al********@newsgroup.nospamwrote in message
news:34**********************************@microsof t.com...
This is a bit of an abuse of this group. Just a nit, but I'm hoping
someone
really good with Regular Expressions can help me out here.

I need to write a regular expression that will do the following:

Search a whole blob of text (including newlines and everything).
Find any text enclosed in brackets ([xyz]), and replace it with a string I
provide and then concatenate the text that had been enclosed in the
brakets,
without the brakets.

So, for example:
Input String:
"Hi there. My name is [Alex]. My brother's name is [Billy]."
Prepend Parameter:
"Mr. "

For this example, the output would be:
"Hi there. My name is Mr. Alex. My brother's name is Mr. Billy."

I'm sure that this is very simple for people who know RegEx's. I ain't one
of those people. Anyone's help would be MOST appreciated.

Alex

Aug 2 '06 #2
An expansion of that, i've gone ahead and written out an example. Also, my
regex is simpler (it will match anything between brackets). If you want just
alpha, replace with "\[[a-zA-Z]+?\]":

Public Sub ShowBillysMessage()
Dim x As System.Text.RegularExpressions.Regex

Dim y As String
y = x.Replace("Hello [Billy], how are you? Please come to [Mike]'s
House.", "\[.+?\]", AddressOf ReplaceStuff)
MessageBox.Show(y)
End Sub

Private Function ReplaceStuff(ByVal m As
System.Text.RegularExpressions.Match) As String
Dim strTemp As String = m.ToString
Const strPrepend As String = "Mr. "

strTemp = strTemp.Trim(New Char() {"[", "]"})
strTemp = strTemp.Insert(0, strPrepend)

Return strTemp
End Function

"Kevin Spencer" wrote:
The Regular Expression is simple:

\[([^][]+)\]

This says that a match consists of the '[' character followed by 1 or more
characters that are neither '[' nor ']' and that is followed by a ']'
character.

It also puts the actual contents of the brackets into a group, Group 1, as
in the following example:

Hi there. My name is [Alex]. My brother's name is [Billy].

In this example, "[Alex]" is a match, and so is "[Billy]". In the "Alex"
match, "Alex" is Group 1. In the "Billy" match, "Billy" is Group 1.

I would use the Regex.Replace overload which takes a string and a
MatchEvaluator delegate. A MatchEvaluator delegate is a method that takes a
System.Text.RegularExpressions.Match object as an argument, and returns the
replacement string for that Match. The Replace method replaces each Match
with the MatchEvaluator, and thus replaces the entire string. It is the
method that does the actual replacing. Example:

// MatchEvaluator delegate
private string bracketReplacer(Match m)
{
// strips the brackets and returns the value enclosed in them.
return m.Groups[1].Value;
}

// Replacement Function
public string ReplaceBrackets(string s)
{
Regex regex = new Regex(@"\[([^][]+)\]");
return regex.Replace(s, new MatchEvaluator(bracketReplacer));
}

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Who is Mighty Abbott? A twin-turret scalawag.

"Alex Maghen" <Al********@newsgroup.nospamwrote in message
news:34**********************************@microsof t.com...
This is a bit of an abuse of this group. Just a nit, but I'm hoping
someone
really good with Regular Expressions can help me out here.

I need to write a regular expression that will do the following:

Search a whole blob of text (including newlines and everything).
Find any text enclosed in brackets ([xyz]), and replace it with a string I
provide and then concatenate the text that had been enclosed in the
brakets,
without the brakets.

So, for example:
Input String:
"Hi there. My name is [Alex]. My brother's name is [Billy]."
Prepend Parameter:
"Mr. "

For this example, the output would be:
"Hi there. My name is Mr. Alex. My brother's name is Mr. Billy."

I'm sure that this is very simple for people who know RegEx's. I ain't one
of those people. Anyone's help would be MOST appreciated.

Alex


Aug 2 '06 #3

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

Similar topics

6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
7
by: Mike Labosh | last post by:
I have the following System.Text.RegularExpressions.Regex that is supposed to remove this predefined list of garbage characters from contact names that come in on import files : Dim...
9
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
4
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
6
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character...
1
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose...
0
by: Support Desk | last post by:
That’s it exactly..thx -----Original Message----- From: Reedick, Andrew Sent: Tuesday, June 03, 2008 9:26 AM To: Support Desk Subject: RE: regex help The regex will now skip anything with...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.