Q: Regular Expressions

G

Geoff Jones

HIya

Can somebody give me some example code to use regular expressions to check
whether string2 has all the characters of string1 at the beginning e.g.

If string1 = "abc" and string2 = "abcxyz" then I get a TRUE result.

Similiarly, if string1 = "g" and string2 = "g9hd67" then I get a TRUE
result.

Thanks in advance

Geoff
 
?

=?ISO-8859-1?Q?=22D=2E_Andr=E9_Dhondt=22?=

Geoff said:
HIya

Can somebody give me some example code to use regular expressions to check
whether string2 has all the characters of string1 at the beginning e.g.

If string1 = "abc" and string2 = "abcxyz" then I get a TRUE result.

Similiarly, if string1 = "g" and string2 = "g9hd67" then I get a TRUE
result.

Thanks in advance

Geoff
I see no need to use RegExp for this--just do

if string1 = string2.Left(String1.Length) then...
 
J

Jay B. Harlow [MVP - Outlook]

Geoff,
As D. André Dhondt suggests, regular expression are not needed per se as you
can use simple string comparisons.

The "easiest" way is to use String.StartsWith.

If string2.StartsWith(string1) Then


Alternatively you could use VB.Strings functions as D. André Dhondt
attempted to show:

Imports VB = Microsoft.VisualBasic

If string1 = VB.Left(string2, VB.Len(string1))

Note: I would avoid mixing System.String methods with VB.Strings methods as
System.String methods are 0 based indexes, while VB.Strings are 1 based
indexes. The "Imports VB = ..." is used so the code works as expected in
Forms. Left is a property of the form as well as a function in the
VB.Strings module.


If for some strange reason you really need to use Regular Expressions. I
would append "^" to the start of the string to form the pattern.

Dim pattern As String = "^" & string1
If Regex.IsMatch(string2, pattern) Then
Debug.WriteLine("regex")
End If

Hope this helps
Jay
 

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