powershellbyexample icon indicating copy to clipboard operation
powershellbyexample copied to clipboard

Examples for [Regex]

Open B-Art opened this issue 1 year ago • 0 comments

Lets split by using replace: (I used = as a replacement so you will remember ;-))

[regex]::Replace('29 July, 2014, 30 July, 2014, 31 July, 2014,29 July, 2014, 30 July, 2014, 31 July, 2014','\s*([^,]+)(,\s*)([^,]+),*\s*', '$1 = $3'+"`n`r")
29 July = 2014
30 July = 2014
31 July = 2014
29 July = 2014
30 July = 2014
31 July = 2014

You can add labels to make it more readable:

[regex]::Replace('29 July, 2014, 30 July, 2014, 31 July, 2014,29 July, 2014, 30 July, 2014, 31 July, 2014','\s*(?<lbl1>[^,]+)(?<lbl2>,\s*)(?<lbl3>[^,]+),*\s*', '${lbl1} = ${lbl3}'+"`n`r")

If you make a typo you will notice this immediately:

[regex]::Replace('29 July, 2014, 30 July, 2014, 31 July, 2014,29 July, 2014, 30 July, 2014, 31 July, 2014','\s*(?<lbl1>[^,]+)(?<lbl2>,\s*)(?<lbl3>[^,]+),*\s*', '${lbl1} = ${lbl}'+"`n`r")
29 July = ${lbl}
30 July = ${lbl}
31 July = ${lbl}
29 July = ${lbl}
30 July = ${lbl}
31 July = ${lbl}

B-Art avatar Jun 19 '24 07:06 B-Art