Xanderu
Xanderu

Reputation: 777

Regex Replacement of part of string

I'm trying to replace a series of strings to extract the date. I'm using powershell and I have an example of part of what I'm trying to achieve:

$Strings = @("this_is_a_test_8_20_2019_test", "this_is_a_test_8_19_2019_test")
$Strings -replace "[^0-9_]",""

Which leaves a result:

____8_20_2019_

____8_19_2019_

I'm trying to isolate just the _ characters that are not followed another _ and then digit. I feel like I'm missing something extremely obvious but I'm not sure what. Anyone have any ideas/suggestions?

Upvotes: 1

Views: 51

Answers (3)

The fourth bird
The fourth bird

Reputation: 163207

One option could be to match 1+ times not a digit \D+ and assert using a positive lookahead (?= what is directly on the right is either an underscore or the end of the string $

In the replacement use an empty string.

\D+(?:_|$)

Regex demo | Try it online

$Strings = @("this_is_a_test_8_20_2019_test", this_is_a_test_8_19_2019_test")
$Strings -replace "\D+(?:_|$)",""

Result

8_20_2019
8_19_2019

Upvotes: 1

Lee_Dailey
Lee_Dailey

Reputation: 7479

presuming that you mean it when you say you want the date [instead of just the number string], this otta do the job. [grin]

what it does ...

  • replaces the non-date string parts with nothing
  • uses ::ParseExact() to convert the date string into a datetime object
    for some reason, the date pattern will match two input digits to one pattern digit, but not one input digit to two pattern digits.

the code ...

$InStuff = @(
    "this_is_a_test_8_20_2019_test"
    "this_is_a_test_10_9_2001_test"
    'test_with_two_digits_12_12_2012_gobbledegook'
    'single_digit_test_1_2_1999_arglebargle'
    )

foreach ($IS_Item in $InStuff)
    {
    $DateString = $IS_Item -replace '^.+_(\d{1,2}_\d{1,2}_\d{4})_.+$', '$1'
    [datetime]::ParseExact($DateString, 'M_d_yyyy', $Null)
    }

output ...

2019 August 20, Tuesday 12:00:00 AM
2001 October 09, Tuesday 12:00:00 AM
2012 December 12, Wednesday 12:00:00 AM
1999 January 02, Saturday 12:00:00 AM

Upvotes: 0

MonkeyZeus
MonkeyZeus

Reputation: 20737

Assuming that you expect:

8_20_2019
8_19_2019

Then you need:

$Strings -replace "[^\d]+_|_[^\d]+",""

Upvotes: 1

Related Questions