Regular Expression issues

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of random commandline random commandline 6 months, 1 week ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #36591
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    Hi,

    I have a variable $datestring which contains a mixture of text and a date. This was pulled from the description field in AD. I would like to evaluate that and convert to date.

    I tested a regular expression '\d{1,2}\/\d{1,2}\/\d{4}$' that extracted the date.
    I first tested it on a fully defined text expression to see if it worked.

    like so

     ([regex]::Match('Disabled 16/3/2016', '\d{1,2}\/\d{1,2}\/\d{4}$' )).value
    16/3/2016
    

    Answer was correct.

    So I thought i could do

     [regex]::Match('$datestring', '\d{1,2}\/\d{1,2}\/\d{4}$' )
    

    But all I got was false.

    How can I get the variable contents be placed in the regex so it can evaluate it? What am i missing?

    Many thanks,

    Wei-Yen Tan

    #36592
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You've put single quotes around $datestring, which is causing that text to be searched literally (not the value of the variable). If it's already a string, no need to quote it:

    [regex]::Match($datestring, '\d{1,2}\/\d{1,2}\/\d{4}$')
    

    You could also just use PowerShell's built-in operator instead of having to call the .net methods:

    $datestring -match '\d{1,2}\/\d{1,2}\/\d{4}$'
    
    #36593
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    Thanks Dave,

    I have tried using the variables without quotes so it looks like this:
    $datestring -match '\d{1,2}\/\d{1,2}\/\d{4}$'

    I revised my variable so it is listed like this:

    $datestring = get-aduser breyes -Properties description | Select -expandproperty description

    So it will expand the properties as a string. Still no dice so I am a bit confused. When I run Get-Member the type is a string.

    #36596
    Profile photo of Bob McCoy
    Bob McCoy
    Participant
    $testString ="This account was created on 02/02/2016 by Bob."
    $pattern = [regex]"(?'date'\d{1,2}/\d{1,2}/\d{4})"
    
    if ($testString -match $pattern)
    {
        # examples of using the match, named and positional captures
        $Matches['date']
        "It happened on $($Matches[0])"
    }
    #36602
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    This pattern: '\d{1,2}\/\d{1,2}\/\d{4}$' requires that the date be the very last thing in the string (that's what the $ anchor at the end means). If your description field has any other characters after the date, even if they're just whitespace, then it wouldn't match. There also can't be any whitespace within the date, such as '1 / 5 / 2014'. If you need to handle those sorts of things, then the pattern needs to be tweaked slightly.

    #36606
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    Hi Dave/Bob,

    That was easy to fix. I just took off the ? and it works as it should.

    Thanks for that. Understanding Regexes is a whole new world...

    #36609
    Profile photo of random commandline
    random commandline
    Participant

    This regular expression may be easier to read.

    # Culture is en-GB (UK)
    $date = 'Disabled 15/3/2016' 
    If ($date -match "Disabled (?'date'.*\d{4})"){Get-Date -Date $Matches['date']}
    
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.