Regex pattern for a string

banu111
banu111 used Ask the Experts™
on
Friends I have string computername=Mytest.Mytest.local
I have a method Extractstring which takes computername

Extractstring(computername)
{

if computername has  pattern like  A.A.Local then do something else nothing
how to determine if the string has .. pattern can i use regex for this



}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
You don't really have to use regex for this. You could get away with using the IndexOf method:

int firstDot = input.IndexOf(".");

if (firstDot >= 0 && firstDot < (input.Length - 1) && input.IndexOf(".", firstDot + 1) >= 0)
{
    // do something
}
else
{
    // nothing
}

Open in new window


If you really want to use a regex, then you can do something like:

if (System.Text.RegularExpressions.Regex.IsMatch(input, @"\.[^.]*\."))
{
    // do something
}
else
{
    // nothing
}

Open in new window

This code should work.
Imports System.Text.RegularExpressions

Module Module1

    Sub Main()
        Dim aStr As String
        aStr = "Mytest.Mytest.Local"

        extractString(aStr)

    End Sub

    Public Sub extractString(ByVal computerName As String)
        Dim regexObj As Regex
        Dim myResult As String

        regexObj = New Regex("\b[a-zA-Z0-9]+\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+\b")

        If regexObj.IsMatch(computerName) Then
            myResult = "Match Found"
        Else
            myResult = "No Match Found"
        End If
    End Sub
End Module

Open in new window

Commented:
If the pattern has to match a repeating name, as in Mytest.Mytest, followed by ".Local" then

Imports System.Text.RegularExpressions

Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim teststring() As String = {"mytest.mytest.Local", "Mytest1.Mytest.local", "A.a.local"}
        For Each s As String In teststring
            ExtractString(s)
        Next
    End Sub
    Private Sub ExtractString(ComputerName As String)
        Dim Pattern As String = "\b(\w+)\.+\1\.Local\b"
        'note the switch that makes the match case insensitive.  Don't use it if it should be case sensitive.
        If Regex.IsMatch(ComputerName, Pattern, RegexOptions.IgnoreCase) Then
            Debug.WriteLine(ComputerName & " is a match!")
        Else
            Debug.WriteLine(ComputerName & " NOT a match!")
        End If
    End Sub

End Class

Open in new window


This gives the output:
mytest.mytest.Local is a match!
Mytest1.Mytest.local NOT a match!
A.a.local is a match!

Note that it can be made case sensitive by changing the parameter in the IsMatch function.
try below regular pattern

^[a-zA-Z]+[.][a-zA-Z]+[.][a-zA-Z]+$

It may helps.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial