All text from camelCase to SNAKE_CASE

I am trying to do some text manipulation using Notepad ++ macros. My final step is converting camelCase strings to SNAKE_CASE. No luck yet. I am not very familiar with regex so I cannot write my own solution.

An example of entering a text file:

firstLine(874),
secondLine(15),
thirdLineOfText87(0x0001);

      

Desired output:

FIRST_LINE(874),
SECOND_LINE(15),
THIRD_LINE_OF_TEXT_87(0x0001);

      

Regex or any plugin is an acceptable answer.

+3


source to share


1 answer


I suggest the following regex approach:

Find what:     (\b[a-z]+|\G(?!^))((?:[A-Z]|\d+)[a-z]*)


Replace: \U\1_\2


Match case: ON .

This will turn the words camelCase87LikeThis

into CAMEL_CASE_87_LIKE_THIS

. If you need to add support for those camel words that start with an uppercase letter, use the following regex modification:

(\G(?!^)|\b[a-zA-Z][a-z]*)([A-Z][a-z]*|\d+)

      

See regex demo (also tested in Notepad ++). Notice the placement \G

inside the regex and the appended A-Z

.



More details

  • (\b[a-z]+|\G(?!^))

    - Group 1, capturing one of two alternatives:
    • \b[a-z]+

      - start of a word ( \b

      is the start word boundary here) followed by 1 + lowercase ASCII letters
    • |

      - or
    • \G(?!^)

      - the ending position of the previous successful match
  • ((?:[A-Z]|\d+)[a-z]*)

    - capture of group 2:
    • (?:[A-Z]|\d+)

      - either an uppercase ASCII letter ( [A-Z]

      ) or ( |

      ) 1 + numbers ( \d+

      )
    • [a-z]*

      - 0 + lowercase ASCII letters.

The replacement pattern \U\1_\2

converts all characters to uppercase with \U

and inserts _

between the two groups (inserted with \1

and \2

backreferences).

enter image description here

+2


source







All Articles