35

Hey I have to change in a lot of places camelCase to snail_case.

I have following example:

billingAddress
paymentDetails

I tried to use find and replace with regex in PhpStorm

In 'find' input field I put in:

([A-Z])

In 'replace' input field I put in:

_\L$1

Result I got:

billing_LAddress
payment_LDetails

What do I need to change in order to get following result:

billing_address
payment_details
0

3 Answers 3

76

First open Find and Replace functionality with CTRL + R and then check the boxes Match Case and Regex (and if necessary In Selection):

enter image description here


1. To replace camelCase to snail_case as in in the question:

find: ([A-Z])
replace: _\l$1

someThing -> some_thing


2. To replace UPPERCASE words to lowercase words use \L

find: (\w*)
replace: \L$1

SOMETHING -> something


3. To replace lowercase words to UPPERCASE words use \U

find: (\w*)
replace: \U$1

something -> SOMETHING


4. To replace first character of words with lowercase use \l

find: (\w*)
replace: \l$1

Something -> something


5. To replace first character of words with UPPERCASE use \u

find: (\w*)
replace: \u$1

something -> Something


Note: Add some extra boundaries

You get best results by adding some additional boundaries that suit your specific case, for example single ' or double quotes " or line breaks \n


Regex Documentation

Check for details on additional Regular Expression Syntax the documentation for PHPStorm or WebStorm.

0
14

According to the PHPstorm docs and according to my test, this works now:

_\l$1
0
3

What do I need to change in order to get following result:

Nothing/Everything. It's simply not supported ATM.

https://youtrack.jetbrains.com/issue/IDEA-70451 -- watch this ticket (star/vote/comment) to get notified on progress.


UPDATE: This functionality is now implemented and available since PhpStorm v10.

1
  • 2
    This comment is outdated. Since Build 142.2822 it is possible as documented here
    – KFO
    Jun 10, 2016 at 9:17

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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