473,394 Members | 1,956 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 questions

I want to use regular expressions to search a string, give the user the
option of replacing, and then maybe replacing the data - using reg
expressions for the search and the replace strings.

However all the Regex replace methods seem to combine in one call the search
and replace.

Is there a way of doing what I want?

Thanks
Nov 21 '05 #1
8 1967
The replace methods [1] do combine the search and replace functionality in
one call.
However, you can use the overloads [2] that take a MatchEvaluator [3]
parameter to pass a reference to a function of yours to be called on each
match [4].
Alternatively you could iterate the matches [5] and use the index [6] and
length [7] properties to act on the original string.

[1]
http://msdn.microsoft.com/library/de...placeTopic.asp
[2]
http://msdn.microsoft.com/library/de...laceTopic1.asp
[3]
http://msdn.microsoft.com/library/de...classtopic.asp
[4]
http://msdn.microsoft.com/library/de...classtopic.asp
[5]
http://msdn.microsoft.com/library/de...tchesTopic.asp
[6]
http://msdn.microsoft.com/library/de...indextopic.asp
[7]
http://msdn.microsoft.com/library/de...engthtopic.asp
Nov 21 '05 #2
> Alternatively you could iterate the matches [5] and use the index [6] and
length [7] properties to act on the original string.

This is what I do now. However, I simply replace the found string with a
replace string.
That is, I do not allow the regular expression type replacements using
groups of characters found in the search. That's what I can't find out how
to do.

Thanks
Nov 21 '05 #3
I'm afraid I don't exactly see what you mean.
Maybe what you're looking for are substitution constructs [1], used to to
put something from the original string into the replacement string?

Could you provide a sample, say "{x} is the original string, and I want to
turn it into {y}".
[1]
http://msdn.microsoft.com/library/de...stitutions.asp
Nov 21 '05 #4
I read your references and need to read them again.
For example, I find the following confusing:
Substitutions are allowed only within replacement patterns. For similar
functionality within regular expressions, use a backreference

Is the term Regular expression only used for the "Find" pattern?

I have been having problems with grouping.
Read the Help I sometimes see something like (\W+) and sometimes {:a+}
I've used the braces in the IDE editor with \1 and that works.

I tried it in my Regex and it doesn't seem to work.
Should it work in Regex?

When is prentices used "(\W+)" and what is the reference construct that
goes with it.

When is $1 used?

Thanks a lot for sticking with me

PS I have the search - ask - replace working.
I search, ask and then replace using the found characters as the string to
search. That is, to replace, I let replace search the string previously
found and then do the replacement.


"Samu Lang" <z> wrote in message
news:u$**************@TK2MSFTNGP09.phx.gbl...
I'm afraid I don't exactly see what you mean.
Maybe what you're looking for are substitution constructs [1], used to to
put something from the original string into the replacement string?

Could you provide a sample, say "{x} is the original string, and I want to
turn it into {y}".
[1]
http://msdn.microsoft.com/library/de...stitutions.asp

Nov 21 '05 #5
Just Me,
I don't believe the RegEx library provides that functionality per se.

It sounds like you want a Replace method on the Match object itself.

If you do, what I would consider doing is do the RegEx.Replace on
Match.Value, (Match.Value is inherited from Capture). It should be the
"entire" string that was found. Where I append the value returned from
RegEx.Replace to a StringBuilder... The trick is going to be, how to collect
the "non-matched parts of the original string... You might be able to
extrapolate these parts from Match.Index & Match.Length.

Something like:

Const pattern As String = "(?<key>\w+)=(?<value>\w+)(:;|)"
Const replacement As String = "The ${key} has value ${value}"

Dim input As String = "a=1;b=2;c=3;d=4;e=5;"

Dim parser As Regex = New Regex(pattern, RegexOptions.Compiled)

Dim match As Match = parser.Match(input)

Dim output As New System.Text.StringBuilder
Dim startIndex As Integer = 0

Debug.WriteLine(input, "input")

Do While match.Success
'Debug.WriteLine(match.Groups("key"), "key")
'Debug.WriteLine(match.Groups("value"), "value")

output.Append(input.Substring(startIndex, match.Index -
startIndex))
output.Append(parser.Replace(match.Value, replacement))

startIndex = match.Index + match.Length

match = match.NextMatch()
Loop

Debug.WriteLine(output.ToString(), "output")

Of course the above loop would be "better" with a MatchEvaluator, however
hopefully it gives you enough to do "un-roll" the loop & what you are
wanting... such as calling the Replace function based on a button click!

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:O7**************@TK2MSFTNGP10.phx.gbl...
Alternatively you could iterate the matches [5] and use the index [6] and
length [7] properties to act on the original string.

This is what I do now. However, I simply replace the found string with a
replace string.
That is, I do not allow the regular expression type replacements using
groups of characters found in the search. That's what I can't find out how
to do.

Thanks

Nov 21 '05 #6
Just Me,
Here is a quick Find & Replace class based on my other sample:

Public Class FindReplace

Private m_parser As Regex
Private m_replacement As String
Private m_input As String
Private m_output As StringBuilder
Private m_startIndex As Integer
Private m_match As Match

Public Property Pattern() As String
Get
Return m_parser.ToString()
End Get
Set(ByVal value As String)
m_parser = New Regex(value, RegexOptions.Compiled)
End Set
End Property

Public Property Replacement() As String
Get
Return m_replacement
End Get
Set(ByVal value As String)
m_replacement = value
End Set
End Property

Public Property Input() As String
Get
Return m_input
End Get
Set(ByVal value As String)
m_input = value
m_output = New StringBuilder
m_startIndex = 0
End Set
End Property

Public ReadOnly Property Output() As String
Get
Return m_output.ToString()
End Get
End Property

Public Function FindNext() As Boolean
If m_match Is Nothing Then
m_match = m_parser.Match(m_input)
Else
m_match = m_match.NextMatch()
End If
Return m_match.Success
End Function

Public Function Replace() As Boolean
If Not m_match.Success Then
FindNext()
End If
If m_match.Success Then
m_output.Append(m_input, m_startIndex, m_match.Index -
m_startIndex)
m_output.Append(m_parser.Replace(m_match.Value,
m_replacement))
m_startIndex = m_match.Index + m_match.Length
End If
Return m_match.Success
End Function

Public Overrides Function ToString() As String
Return m_output.ToString()
End Function

End Class

Public Sub Main()
Dim find As New FindReplace
find.Pattern = "(?<key>\w+)=(?<value>\w+)(:;|)"
find.Replacement = "Key ${key} has value ${value}"
find.Input = "a=1;b=2;c=3;d=4;e=5;"

Do While find.Replace()

Loop
Debug.WriteLine(find.ToString(), "output")

End Sub

Alternate loop that should also work:

Do While find.FindNext()
find.Replace()
Loop

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:O7**************@TK2MSFTNGP10.phx.gbl...
Alternatively you could iterate the matches [5] and use the index [6] and
length [7] properties to act on the original string.

This is what I do now. However, I simply replace the found string with a
replace string.
That is, I do not allow the regular expression type replacements using
groups of characters found in the search. That's what I can't find out how
to do.

Thanks

Nov 21 '05 #7
Doh!

The Replace function had a bug in it, here is a replacement:

Public Function Replace() As Boolean
If (m_match Is Nothing) _
OrElse (Not m_match.Success) _
OrElse (m_startIndex > m_match.Index) Then
FindNext()
End If
If m_match.Success Then
m_output.Append(m_input, m_startIndex, m_match.Index -
m_startIndex)
m_output.Append(m_parser.Replace(m_match.Value,
m_replacement))
m_startIndex = m_match.Index + m_match.Length
End If
Return m_match.Success
End Function

The above Replace will enable the following to work as expected:

Do While Replace()
' This space intentionally left blank.
Loop

Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eK**************@TK2MSFTNGP09.phx.gbl...
Just Me,
Here is a quick Find & Replace class based on my other sample:

Public Class FindReplace

Private m_parser As Regex
Private m_replacement As String
Private m_input As String
Private m_output As StringBuilder
Private m_startIndex As Integer
Private m_match As Match

Public Property Pattern() As String
Get
Return m_parser.ToString()
End Get
Set(ByVal value As String)
m_parser = New Regex(value, RegexOptions.Compiled)
End Set
End Property

Public Property Replacement() As String
Get
Return m_replacement
End Get
Set(ByVal value As String)
m_replacement = value
End Set
End Property

Public Property Input() As String
Get
Return m_input
End Get
Set(ByVal value As String)
m_input = value
m_output = New StringBuilder
m_startIndex = 0
End Set
End Property

Public ReadOnly Property Output() As String
Get
Return m_output.ToString()
End Get
End Property

Public Function FindNext() As Boolean
If m_match Is Nothing Then
m_match = m_parser.Match(m_input)
Else
m_match = m_match.NextMatch()
End If
Return m_match.Success
End Function

Public Function Replace() As Boolean
If Not m_match.Success Then
FindNext()
End If
If m_match.Success Then
m_output.Append(m_input, m_startIndex, m_match.Index -
m_startIndex)
m_output.Append(m_parser.Replace(m_match.Value,
m_replacement))
m_startIndex = m_match.Index + m_match.Length
End If
Return m_match.Success
End Function

Public Overrides Function ToString() As String
Return m_output.ToString()
End Function

End Class

Public Sub Main()
Dim find As New FindReplace
find.Pattern = "(?<key>\w+)=(?<value>\w+)(:;|)"
find.Replacement = "Key ${key} has value ${value}"
find.Input = "a=1;b=2;c=3;d=4;e=5;"

Do While find.Replace()

Loop
Debug.WriteLine(find.ToString(), "output")

End Sub

Alternate loop that should also work:

Do While find.FindNext()
find.Replace()
Loop

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:O7**************@TK2MSFTNGP10.phx.gbl...
Alternatively you could iterate the matches [5] and use the index [6]
and length [7] properties to act on the original string.

This is what I do now. However, I simply replace the found string with a
replace string.
That is, I do not allow the regular expression type replacements using
groups of characters found in the search. That's what I can't find out
how to do.

Thanks


Nov 21 '05 #8
Wow - I'm going to go off-line and study this.

Thanks

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Doh!

The Replace function had a bug in it, here is a replacement:

Public Function Replace() As Boolean
If (m_match Is Nothing) _
OrElse (Not m_match.Success) _
OrElse (m_startIndex > m_match.Index) Then
FindNext()
End If
If m_match.Success Then
m_output.Append(m_input, m_startIndex, m_match.Index -
m_startIndex)
m_output.Append(m_parser.Replace(m_match.Value,
m_replacement))
m_startIndex = m_match.Index + m_match.Length
End If
Return m_match.Success
End Function

The above Replace will enable the following to work as expected:

Do While Replace()
' This space intentionally left blank.
Loop

Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eK**************@TK2MSFTNGP09.phx.gbl...
Just Me,
Here is a quick Find & Replace class based on my other sample:

Public Class FindReplace

Private m_parser As Regex
Private m_replacement As String
Private m_input As String
Private m_output As StringBuilder
Private m_startIndex As Integer
Private m_match As Match

Public Property Pattern() As String
Get
Return m_parser.ToString()
End Get
Set(ByVal value As String)
m_parser = New Regex(value, RegexOptions.Compiled)
End Set
End Property

Public Property Replacement() As String
Get
Return m_replacement
End Get
Set(ByVal value As String)
m_replacement = value
End Set
End Property

Public Property Input() As String
Get
Return m_input
End Get
Set(ByVal value As String)
m_input = value
m_output = New StringBuilder
m_startIndex = 0
End Set
End Property

Public ReadOnly Property Output() As String
Get
Return m_output.ToString()
End Get
End Property

Public Function FindNext() As Boolean
If m_match Is Nothing Then
m_match = m_parser.Match(m_input)
Else
m_match = m_match.NextMatch()
End If
Return m_match.Success
End Function

Public Function Replace() As Boolean
If Not m_match.Success Then
FindNext()
End If
If m_match.Success Then
m_output.Append(m_input, m_startIndex, m_match.Index -
m_startIndex)
m_output.Append(m_parser.Replace(m_match.Value,
m_replacement))
m_startIndex = m_match.Index + m_match.Length
End If
Return m_match.Success
End Function

Public Overrides Function ToString() As String
Return m_output.ToString()
End Function

End Class

Public Sub Main()
Dim find As New FindReplace
find.Pattern = "(?<key>\w+)=(?<value>\w+)(:;|)"
find.Replacement = "Key ${key} has value ${value}"
find.Input = "a=1;b=2;c=3;d=4;e=5;"

Do While find.Replace()

Loop
Debug.WriteLine(find.ToString(), "output")

End Sub

Alternate loop that should also work:

Do While find.FindNext()
find.Replace()
Loop

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:O7**************@TK2MSFTNGP10.phx.gbl...
Alternatively you could iterate the matches [5] and use the index [6]
and length [7] properties to act on the original string.

This is what I do now. However, I simply replace the found string with a
replace string.
That is, I do not allow the regular expression type replacements using
groups of characters found in the search. That's what I can't find out
how to do.

Thanks



Nov 21 '05 #9

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

Similar topics

1
by: rdimayuga | last post by:
I need a regex pattern that will match a string starting with zero or one dot's. For example, ".string" and "string" should both match, but something like "estring" should not match. So far, I've...
4
by: Michael Vilain | last post by:
Originally, I was using $value =~ s/<.*>//g; to strip HTML tags from a variable. It actually stripped everything from the first "<" to the last ">" after the ending tag. I found this regex...
2
by: Richard Latter | last post by:
Hello All, I am a newbie to the Boost library and I have a question about a simple function. All I would like to do is to create a simple function that can test strings using regular...
4
by: H | last post by:
This is kind of an followup on oneof my previous questions, and it has with RegEx to do. I have a string containing of several words. What would a good regex expression looklike to get one match...
1
by: Leon | last post by:
Please help, what I'm I doing wrong????? --> This following code will not run correctly, no syntax errors, but.. --> execution error = The subdomain does not exist in database.... "but it does in...
4
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the...
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...
1
by: Dan Holmes | last post by:
i have this: new System.Text.RegularExpressions.Regex("^\d+$") // \d by itself failed as well. but the compiler generates "unrecognized escape sequence". so i changed it to this: new...
7
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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.