I want a Powershell regex replace command to convert strings such as these…
<code>RedHerringFallacy
SunkenCostFallacy
FalseDilemmaLogic
AnyOtherCamelCapString
</code>
<code>RedHerringFallacy
SunkenCostFallacy
FalseDilemmaLogic
AnyOtherCamelCapString
</code>
RedHerringFallacy
SunkenCostFallacy
FalseDilemmaLogic
AnyOtherCamelCapString
to…
<code>Red_Herring_Fallacy
Sunken_Cost_Fallacy
False_Dilemma_Logic
Any_Other_Camel_Cap_String
</code>
<code>Red_Herring_Fallacy
Sunken_Cost_Fallacy
False_Dilemma_Logic
Any_Other_Camel_Cap_String
</code>
Red_Herring_Fallacy
Sunken_Cost_Fallacy
False_Dilemma_Logic
Any_Other_Camel_Cap_String
Here are all my unsuccessful attempts:
1) The Lookbehind
<code>$string = "FundRegions"
$modifiedString = $string -replace '(?<=[a-z])(?=[A-Z])', '_'
$modifiedString
</code>
<code>$string = "FundRegions"
$modifiedString = $string -replace '(?<=[a-z])(?=[A-Z])', '_'
$modifiedString
</code>
$string = "FundRegions"
$modifiedString = $string -replace '(?<=[a-z])(?=[A-Z])', '_'
$modifiedString
Gives me: F_u_n_d_R_e_g_i_o_n_s
2) Plain and Simple
<code>$string = "FundRegions"
$modifiedString = $string -replace '([a-z])([A-Z])', '$1_$2'
$modifiedString
</code>
<code>$string = "FundRegions"
$modifiedString = $string -replace '([a-z])([A-Z])', '$1_$2'
$modifiedString
</code>
$string = "FundRegions"
$modifiedString = $string -replace '([a-z])([A-Z])', '$1_$2'
$modifiedString
Gives me: F_un_dR_eg_io_ns
3) There’s No Going Backticks
<code>$string = "FundRegions"
$modifiedString = $string -replace '([a-z])([A-Z])', '`$1_`$2'
$modifiedString
</code>
<code>$string = "FundRegions"
$modifiedString = $string -replace '([a-z])([A-Z])', '`$1_`$2'
$modifiedString
</code>
$string = "FundRegions"
$modifiedString = $string -replace '([a-z])([A-Z])', '`$1_`$2'
$modifiedString
Gives me: `F_`u`n_`d`R_`e`g_`i`o_`ns
4) One Character at a Time
<code>$string = "FundRegions"
function InsertUnderscores([string]$str) {
$result = ""
for ($i = 0; $i -lt $str.Length; $i++) {
$result += $str[$i]
if ($i -lt $str.Length - 1 -and $str[$i] -match '[a-z]' -and $str[$i+1] -match '[A-Z]') {
$result += '_'
}
}
return $result
}
$modifiedString = InsertUnderscores $string
$modifiedString
</code>
<code>$string = "FundRegions"
function InsertUnderscores([string]$str) {
$result = ""
for ($i = 0; $i -lt $str.Length; $i++) {
$result += $str[$i]
if ($i -lt $str.Length - 1 -and $str[$i] -match '[a-z]' -and $str[$i+1] -match '[A-Z]') {
$result += '_'
}
}
return $result
}
$modifiedString = InsertUnderscores $string
$modifiedString
</code>
$string = "FundRegions"
function InsertUnderscores([string]$str) {
$result = ""
for ($i = 0; $i -lt $str.Length; $i++) {
$result += $str[$i]
if ($i -lt $str.Length - 1 -and $str[$i] -match '[a-z]' -and $str[$i+1] -match '[A-Z]') {
$result += '_'
}
}
return $result
}
$modifiedString = InsertUnderscores $string
$modifiedString
Gives me: F_u_n_d_R_e_g_i_o_n_s
Calgon, take me away! Please help. Thank you.
New contributor
Jerome B is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.