As my first project, I have a text string which contains (among other things) dates formatted as dd/mm/yyyy
I want to build a script that will find the index location of each instance where these are found in the text, and just before that index location, insert a delimiter character.
This produced the output of groups from which I extracted the index location for each date substring. I then created an array variable with all of the index values.
I then converted those arrayed string values to numbers
[array]$c = foreach($number in $STRARRAY) {([int]::parse($number))}
Now I need one more loop to insert a delimiter character at the location of each index.
Can anyone suggest how that command would be formed?
As you are a Powershell beginner I’d recommend staying with the built-in cmdlets as long as possible. It makes the code easier to read, easier to debug, easier to maintain or extend. And some dotNet methods have slightly suprising side effects compaired to standard Powershell cmdlets.
I think you’re overcomplicating your task.
If I got you right and assumed your input comes from a file called text.txt and your delimiter charachter is “#” you can achieve your task like this:
If you want to insert a delimiter, such as a semi-colon, you can do this with a pretty simple process:
$string = "this is a test 01/01/2020 testing 01/02/2020 "
#https://www.regular-expressions.info/dates.html
$pattern = '(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d'
#Place a semicolon in front of match 0
$string -replace $pattern, ';$0'
The regex matches:
PS C:\Users\rasim> $Matches
Name Value
---- -----
3 20
2 01
1 01
0 01/01/2020
Great. Because it worked here in my tests so I was worried. Thanks.
The trick in my code snippet is in the regex pattern. It’s called look-ahead and searches for something “followed by something particular”. So it matches actually the charachter before (or after when you use a look-behind) the pattern you provide this way … what you wanted.
Thank you for explaining that - and Look-aheads have been something I’ve had difficulty getting my brain around. I can see I’m going to have to dig into that and get a solid understanding of it.