By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Getting variable in a pattern using Regex

P: n/a
Hi,
I have a string with some fixed text and variable text.
For example: "this is a fixed text THE NEEDED INFO more more fixed text".
How do I get the the variable text (THE NEEDED INFO) from this string ?
A simple example will help.

Thanks.
ra***@hotmail.com
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Dim txt As String
txt = "this is a fixed text THE NEEDED INFO more more fixed text"
txt = Mid(txt, 23, txt.Length - 22)

23 <- starting index
txt.Length - 22 <- size of your NEEDED INFO

"Ya Ya" wrote:
Hi,
I have a string with some fixed text and variable text.
For example: "this is a fixed text THE NEEDED INFO more more fixed text".
How do I get the the variable text (THE NEEDED INFO) from this string ?
A simple example will help.

Thanks.
ra***@hotmail.com

Nov 21 '05 #2

P: n/a
Thanks, but I am looking for example using REGEX.

ra***@hotmail.com

"Alex Levi" <Al******@discussions.microsoft.com> wrote in message
news:44**********************************@microsof t.com...
Dim txt As String
txt = "this is a fixed text THE NEEDED INFO more more fixed text"
txt = Mid(txt, 23, txt.Length - 22)

23 <- starting index
txt.Length - 22 <- size of your NEEDED INFO

"Ya Ya" wrote:
Hi,
I have a string with some fixed text and variable text.
For example: "this is a fixed text THE NEEDED INFO more more fixed text".
How do I get the the variable text (THE NEEDED INFO) from this string ?
A simple example will help.

Thanks.
ra***@hotmail.com

Nov 21 '05 #3

P: n/a
Dim text As String = "One car red car blue car"
Dim pat As String = "(\w+)\s+(car)"
' Compile the regular expression.
Dim r As Regex = new Regex(pat, RegexOptions.IgnoreCase)
' Match the regular expression pattern against a text string.
Dim m As Match = r.Match(text)
Dim matchcount as Integer = 0
While (m.Success)
matchCount += 1
Console.WriteLine("Match" & (matchCount))
Dim i As Integer
For i = 1 to 2
Dim g as Group = m.Groups(i)
Console.WriteLine("Group" & i & "='" & g.ToString() & "'")
Dim cc As CaptureCollection = g.Captures
Dim j As Integer
For j = 0 to cc.Count - 1
Dim c As Capture = cc(j)
Console.WriteLine("Capture" & j & "='" & c.ToString() _
& "', Position=" & c.Index)
Next j
Next i
m = m.NextMatch()
End While
chanmm
"Ya Ya" <ra***@hotmail.com> wrote in message
news:uU*************@TK2MSFTNGP12.phx.gbl...
Thanks, but I am looking for example using REGEX.

ra***@hotmail.com

"Alex Levi" <Al******@discussions.microsoft.com> wrote in message
news:44**********************************@microsof t.com...
Dim txt As String
txt = "this is a fixed text THE NEEDED INFO more more fixed text"
txt = Mid(txt, 23, txt.Length - 22)

23 <- starting index
txt.Length - 22 <- size of your NEEDED INFO

"Ya Ya" wrote:
Hi,
I have a string with some fixed text and variable text.
For example: "this is a fixed text THE NEEDED INFO more more fixed
text".
How do I get the the variable text (THE NEEDED INFO) from this string ?
A simple example will help.

Thanks.
ra***@hotmail.com


Nov 21 '05 #4

P: n/a
On 2004-10-23, Ya Ya <ra***@hotmail.com> wrote:
Hi,
I have a string with some fixed text and variable text.
For example: "this is a fixed text THE NEEDED INFO more more fixed text".
How do I get the the variable text (THE NEEDED INFO) from this string ?
A simple example will help.


Assuming you want to match by the surrounding text (as opposed to
matching all case, or whatever)...
Private Function DoMatch(ByVal input As String) As String
Dim pattern As String = "fixed text ([\w ]+) more more fixed text"

Dim match As System.Text.RegularExpressions.Match = _
System.Text.RegularExpressions.Regex.Match(input, pattern)

If match.Length > 0 Then
Return match.Groups(1).Value
Else
Return Nothing
End If

End Function
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.