Replace on a case change
You are looking for this (I removed the space before ID, assuming it a typo)
(?:(?<=[a-z])(?=[A-Z]))|(?:(?<=[A-Z])(?=[a-z]))
It will transform personalIDnumber
into personal_ID_number
See it here on Regexr
the Lookbehind ((?<=[a-z])
) and lookahead ((?=[A-Z])
) construction is matching the empty string between a lower case and a uppercase letter (or the other way round in the second part after the pipe |
) and replace this with a underscore.
Replace on a case change with optional whitespace
If you want to include whitespace in the replace, just include it between the lookarounds
(?:(?<=[a-z])\s*(?=[A-Z]))|(?:(?<=[A-Z])\s*(?=[a-z]))
It will transform personal IDnumber
into personal_ID_number
See this here on Regexr
Replace on a case change, but each word starts with an uppercase
If you say every word starts with an uppercase letter, then you can do this
(?:(?<=[a-z])(?=[A-Z]))|(?:(?<=[A-Z])(?=[A-Z][a-z]))
This will make from
PersonalIDNumber
JustAnotherTest
OtherWordWithID
SomeMORETest
this
Personal_ID_Number
Just_Another_Test
Other_Word_With_ID
Some_MORE_Test
See this here on Regexr