Regular Expressions problem

  • Thread starter Thread starter Marcos Góis
  • Start date Start date
M

Marcos Góis

Can sombody explain me, why the following code has two different output
values?

---

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Text.RegularExpressions

Public Module MyModule
Sub Main
Dim MyInt As Integer = 234
Dim MyRegex As Regex = New Regex("234987234")

Console.WriteLine( _
MyRegex.IsMatch("[0-9]") _
)

Console.WriteLine(Regex.IsMatch("234987234", _
"[0-9]"))

Console.ReadLine()
End Sub
End Module
 
Marcos:

In the first of your two examples, you're creating a new RegEx using the
code:

Dim MyRegex As Regex = New Regex("234987234")

But that's incorrect. Your code is searching for the pattern "234987234" in
the string "[0-9]". The parameter should be the pattern, not the searched
string. Instead, change it to the expression and then pass the searched
string to the IsMatch method as a parameter:

Dim MyRegex As Regex = New Regex("[0-9]")

Console.WriteLine( _
MyRegex.IsMatch("234987234") _
)

In the corrected version, the output matches that of your second example and
both return True.

Thanks,
Russell Jones

Marcos Góis said:
By the way, the ouput is:

False
True

You can use the Snippet Compiler
(http://www.sliver.com/dotnet/SnippetCompiler/) to avoid creating a new
project

Thanks

Marcos Góis said:
Can sombody explain me, why the following code has two different output
values?

---

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Text.RegularExpressions

Public Module MyModule
Sub Main
Dim MyInt As Integer = 234
Dim MyRegex As Regex = New Regex("234987234")

Console.WriteLine( _
MyRegex.IsMatch("[0-9]") _
)

Console.WriteLine(Regex.IsMatch("234987234", _
"[0-9]"))

Console.ReadLine()
End Sub
End Module
 

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

Back
Top