Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pattern puzzle

Status
Not open for further replies.

digiduck

Programmer
Apr 4, 2003
95
US
In the following example #### represents any number of varying length.

Code:
{\cf11\super ####}

I need a way to run through a string like...

Code:
This is a {\cf11\super 87} number for you {\cf11\super 653}.

...and replace the {\cf11\super ####} with just the number where the #### is. The result of the above would then be...

Code:
This is a 87 number for you 653.

Any help would be greatly appreciated. Thank You.

Gone looking for myself. Should I arrive before I'm back, keep me here.
 
I should clarify that in the process of replacing each result I need to store the #### in a variable so I can't simple search for "{\cf11\super" and "}" and remove them.

Gone looking for myself. Should I arrive before I'm back, keep me here.
 
Hi,

I've never figured out how to use part of the match in a regular expression replacement. If anybody does, that would definietly be a better solution and I would be interested in seeing it...
Meanwhile this works as well
---------------------------------------------------

Dim MyRegEx As New Regex(&quot;(?<MyPat>\{\\cf11\\super (\d){1,4}\})&quot;)
Dim i, l As Integer
Dim sr, st As String
Dim s As String = &quot;This is a {\cf11\super 87} number for you {\cf11\super 653}.&quot;
Dim Mc As MatchCollection = MyRegEx.Matches(s)
Dim M As Match
If Mc.Count > 0 Then
l = Mc(0).Index + Mc(0).Length
sr = s.Substring(0, Mc(0).Index)
For i = 1 To Mc.Count - 1
st = Mc(i - 1).Groups(&quot;MyPat&quot;).Value
sr = sr & st.Substring(13, st.Length - 14) & s.Substring(l, Mc(i).Index - l)
l = Mc(i).Index + Mc(i).Length
Next i
st = Mc(i - 1).Groups(&quot;MyPat&quot;).Value
sr = sr & st.Substring(13, st.Length - 14) & s.Substring(l) & vbCrLf
Else
sr = s
End If
MessageBox.Show(sr)
---------------------------------------------------


Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top