Regex Help?

G

Guest

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
 
K

Kevin Spencer

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.
 
G

Guest

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 said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top