3

Our standards have changed and I want to do a 'find and replace' in say Dreamweaver(it allows for RegEx or we just got Visual Studio 2010, if it allows for searching by RegEx) for all the underscores and camelCase them.

What would be the RegEx to do that?

RegEx baffles me. I definitely need to do some studying.

Thanks in advance!

Update: A little more info - I'm searching within my html,aspx,cfm or css documents for any string that contains an underscore and replacing it with the following letter capitalized.

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • 1
    Let's be specific. Can you give an example of your "source" and "target" data? Also, it may be that what you want is possible with a regex, but not in the regex implementation that DreamWeaver uses. Are you open to using other tools? – ghoti Mar 28 '12 at 16:51
  • Ah good point. I'm searching within my html,aspx,cfm or css documents for any string that contains an underscore and replacing it with the following letter capitalized. I would use whatever works. I'm not beholden to Dreamweaver by any means. – Richard Austin Mar 28 '12 at 17:01

4 Answers4

3

I had this problem, but I need to also handle converting fields like gap_in_cover_period_d_last_5_yr into gapInCoverPeriodDLast and found 9 out of 10 other sed expressions, don't like 1 letter words or numbers.

So to answer the question, use sed. sed -s is the equivalent to using the :s command in vim. So the example below is a command (ie sed -s/.../gc

This seemed to work, although I did have to run it twice (word_a_word will become wordA_word on the first pass and wordAWord on the second. sed backward commands, are just too magical for my muggle blood):-

s/\([A-Za-z0-9]\+\)_\([0-9a-z]\)/\1\U\2/gc
sibaz
  • 1,242
  • 14
  • 26
2

I recently had to approach a similar situation you asked about. Here is a regex I've been using in VIM which does the job for me:

%s/_\([a-zA-Z]\)/\u\1/g

As an example:

this_is_a_test becomes thisIsATest

Diemuzi
  • 3,507
  • 7
  • 36
  • 61
0

I have a solution in PHP

preg_replace("/(_)(.)/e", "strtoupper('\\2')", $str)

There may be a more elegant selector criteria but I wanted to keep it simple.

joe
  • 1
0

I don't think there is a good way to do this purely with regex. Searching for _ characters is easy, something like ._. should work to find an _ with something on either side, but you need a more powerful scripting system to change the case of the character following the _. I suggest perl :)

Adam Shiemke
  • 3,734
  • 2
  • 22
  • 23
  • This is a good point. I don't think it can work in Dreamweaver. It will Find: the underscores, but it can't 'hold on' to the letter following the underscore in the Replace: – Richard Austin Mar 28 '12 at 17:13
  • hmmm if you could just find: the letter after an underscore and then uppercase that letter in the replace: In the string **Adam_shiemke** your example ._. would get **m_s** I just want the **s** and then in another statement to uppercase that **s** (so _uppercase $1_). Do you know how that could be written? – Richard Austin Mar 28 '12 at 17:53