Link to home
Start Free TrialLog in
Avatar of danncox
danncoxFlag for Canada

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:
$GroupNameProper = $GroupName -creplace "(\B[A-Z])", ' $1'

Open in new window

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...
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

try
$GroupNameProper = $GroupName -creplace "(\B[A-Z]+)([A-Z][a-z])", '$1 $2'

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of danncox

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
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!
Avatar of danncox

ASKER

Thanks, TerryAtOpus.  Not entirely clear, but enough for me to work it out, I think.