danncox
asked on
Regular Expressions question
I am having trouble refining a regex query. I am using it in a PowerShell script to convert a camel-case group name to "regular text". e.g. TestGroup to Test Group. I have been using the following code to do this:
Unfortunately, my experience with Regex is too limited. The answer is probably obvious, but I'm just not getting it...
$GroupNameProper = $GroupName -creplace "(\B[A-Z])", ' $1'
In most cases this works, unless the "proper" name has two capitals in a row. For example, HRSecretary becomes H R Secretary, rather then the intended HR Secretary. I need to place a space between capitalized letters ONLY if the next character is lower-case.Unfortunately, my experience with Regex is too limited. The answer is probably obvious, but I'm just not getting it...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the quick replies!
I'm going to accept the solution from TerryAtOpus because it worked in all my test examples. The solution from lwadwell was close, but missed on ones like this: St-ExecutiveSecretary, where it produced ST-ExecutiveSecretary again, rather than the expected ST-Executive Secretary.
TerryAtOpus - any chance you could explain your solution? :-)
Dann
I'm going to accept the solution from TerryAtOpus because it worked in all my test examples. The solution from lwadwell was close, but missed on ones like this: St-ExecutiveSecretary, where it produced ST-ExecutiveSecretary again, rather than the expected ST-Executive Secretary.
TerryAtOpus - any chance you could explain your solution? :-)
Dann
Heres a generated explanation from www.myregextester.com (produced by checking the "Explain" checkbox)... is that understandable? If not, let me know.
( group and capture to $1:
-------------------------- ---------- ---------- ---------- ---------- ----
\b the boundary between a word char (\w)
and something that is not a word char
-------------------------- ---------- ---------- ---------- ---------- ----
[A-Z]+ any character of: 'A' to 'Z' (1 or more
times (matching the most amount
possible))
-------------------------- ---------- ---------- ---------- ---------- ----
[a-z]* any character of: 'a' to 'z' (0 or more
times (matching the most amount
possible))
-------------------------- ---------- ---------- ---------- ---------- ----
) end of \1
-------------------------- ---------- ---------- ---------- ---------- ----
( group and capture to $2:
-------------------------- ---------- ---------- ---------- ---------- ----
[A-Z] any character of: 'A' to 'Z'
-------------------------- ---------- ---------- ---------- ---------- ----
[a-z] any character of: 'a' to 'z'
-------------------------- ---------- ---------- ---------- ---------- ----
) end of \2
Thanks for the points!
( group and capture to $1:
--------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------
[A-Z]+ any character of: 'A' to 'Z' (1 or more
times (matching the most amount
possible))
--------------------------
[a-z]* any character of: 'a' to 'z' (0 or more
times (matching the most amount
possible))
--------------------------
) end of \1
--------------------------
( group and capture to $2:
--------------------------
[A-Z] any character of: 'A' to 'Z'
--------------------------
[a-z] any character of: 'a' to 'z'
--------------------------
) end of \2
Thanks for the points!
ASKER
Thanks, TerryAtOpus. Not entirely clear, but enough for me to work it out, I think.
Open in new window