Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need a regex statement to find any words formatted in CamelCase. Examples include:

  • CamelCase.
  • Trunk_Note.
  • Med-Dr.Zung.
  • _MyToDo-Housework.
  • _MyToDo-Vacation_Cancun
  • iOS_GReader

Key points:

  • The first character can be any character that is valid in a Windows OS filename.
  • Any character can be any character that is valid in a Windows OS filename including ..
  • The word will be delimited by a preceding (space), (, or [ and followed by a (space), ), or ].

Why? I use an iOS app called Trunk Notes to maintain extensive notes on all aspects of my life (1000+ topics covering 10+ years of work, family, and personal content). Trunk Notes uses Markdown for content formatting but has a specific problem with my CamelCase words. When placed inside a link label or title, e.g. iOS_WriteRoom, the Markdown engine chokes and generates broken HTML. Underscores (_) in the words also confuse the engine. The simplist solution is to precede the offending word with /, e.g. /iOS_WriteRoom.

I began making manual edits but realize regex should work for me. So my regex solution will look for the matching pattern and prepend a / if a / has not already been prepended.

Thank you in advance for any suggestions.

share|improve this question
2  
Trunk_Note and Med-Dr.Zung are not camel case, they're snake case or mixed camel / snake case. The only camel case example you have is "CamelCase". The others contain underscores, dashes, or other punctuation to separate words, where camel case only uses capitalization: TrunkNote, MedDrZung, MyToDoHousework, MyToDoVacationCancun, iOSGReader. Food for thought! –  Darth Android Jun 26 '13 at 20:04

2 Answers 2

This is particularly hard because you don't seem to be following a CamelCase style where the first letter is always capitalized and you are allowing multiple, consecutive capitals. The only definition that would capture all of your examples would be

Words containing at least two non-consecutive capital letters separated by 
lower case letters.

This means that it will also capture patterns like foObaR which are not really CamelCase and might not be what you need.

Anyway, the regular expression below matches all your examples*:

 [\(\[ ]([_\-\.a-z]*[A-Z][_\-\.a-zA-Z]+[A-Z]+[_\-\.a-z]*)[\)\] ]
  ----- - -------- - ---  ----------- - --- - -------- -- -----
    |   |     |    |  |        |      |  |  |     |    ||   |> Right boundary: 
    |   |     |    |  |        |      |  |  |     |    ||         ),] or space
    |   |     |    |  |        |      |  |  |     |    ||----> Close capture
    |   |     |    |  |        |      |  |  |     |    |-----> 0 or more
    |   |     |    |  |        |      |  |  |     |----------> Character class:
    |   |     |    |  |        |      |  |  |                   ,-,., or a to z
    |   |     |    |  |        |      |  |  |----------------> 1 or more    
    |   |     |    |  |        |      |  |-------------------> A to Z       
    |   |     |    |  |        |      |----------------------> 1 or more 
    |   |     |    |  |        |-----------------------------> Character class:
    |   |     |    |  |                                        ,-,., a to z 
    |   |     |    |  |                                        or A to Z
    |   |     |    |  |--------------------------------------> Capital letter
    |   |     |    |-----------------------------------------> 0 or more
    |   |     |----------------------------------------------> Character class:
    |   |                                                      ,-,., and a to z
    |   |----------------------------------------------------> Open capture
    |--------------------------------------------------------> Left boundary:
                                                               (,[ or space

* I think. I have not used notepad++ myself but their site says they use Perl Compatible Regular Expressions. The above is a PCRE that matches all your examples.

share|improve this answer

Craig --

I don't think the one above will work for all your cases in Notepad++. If you haven't found an answer yet, try this variation:

([(\[\s])([a-z0-9_.\-]*[A-Z]+[a-z0-9_.\-]+[A-Z]+[a-z0-9_.\-]*)([)\]\s])

This will also match the boundary characters -- [], (), or space (\s) -- to make sure you're getting only the WikiWords you want to catch. Use that string in the Find field and this one in Replace:

\1/\2\3

... and you should end up with (e.g.) (/MyWikiWords) in place of (MyWikiWords).


If you have some WikiWords that appear outsidet the context of brackets or parentheses, you could try this Search:

\b([a-z0-9_.\-]*[A-Z]+[a-z0-9_.\-]+[A-Z]+[a-z0-9_.\-]*)\b

... and this in Replace:

/\1

... which use the word boundary (\b) regex.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.