• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

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...
0
danncox
Asked:
danncox
  • 2
  • 2
1 Solution
 
lwadwellCommented:
try
$GroupNameProper = $GroupName -creplace "(\B[A-Z]+)([A-Z][a-z])", '$1 $2'

Open in new window

0
 
Terry WoodsIT GuruCommented:
How about this?

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

Open in new window

0
 
danncoxAuthor Commented:
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
0
 
Terry WoodsIT GuruCommented:
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!
0
 
danncoxAuthor Commented:
Thanks, TerryAtOpus.  Not entirely clear, but enough for me to work it out, I think.
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now