The only regex that changes the camel case to lowercase

I am trying to write a SublimeText snippet for javascript import statements. I want the format to be as follows:

 import MyFooBar from 'my-foo-bar'; 

The input to my regex is MyFooBar , and the output should be my-foo-bar . I found an answer that almost works in Regex - CamelCase in lower case with underscores :

Search

((?<=.)[AZ][a-zA-Z]*)|((?<=[a-zA-Z])\d+)

Replace

-$1$2

The answer says simply to use the javascript .toLowerCase() method for the subscript, but the Perl fragments use perl, of which I have the shortest knowledge. A quick search said that for the bottom, I can use \L at the beginning of my replacement.

 /((?<=.)[AZ][a-zA-Z0-9]*)|((?<=[a-zA-Z])\d+)/\L-\1\2/g 

This works on everyone but the character of the first segment, so MyFooBar becomes my-foo-bar .

I thought maybe I can run two regular expressions in a sequence, but either perl or Sublime will not recognize this.

Thoughts?

Edit:

When I say that it uses perl, I just mean that it uses the perl regex. As far as I can tell, I cannot execute arbitrary code; I can only specify a regex that perl can do.

Here is the full text of my snippet:

 <snippet> <content><![CDATA[ import ${1:module} from '${2:./path/}${1/((?<=.)[AZ][a-zA-Z0-9]*)|((?<=[a-zA-Z])\d+)/\L-\1\2/g}'; ]]></content> <!-- Optional: Set a tabTrigger to define how to trigger the snippet --> <tabTrigger>import</tabTrigger> <!-- Optional: Set a scope to limit where the snippet will trigger --> <scope>source.js</scope> </snippet> 
+5
source share
3 answers

I changed RegEx to something more useful and possibly simple.

** Updated

 <snippet> <content><![CDATA[ import ${1:module} from '${2:./path/}${1/(^[AZ][az]+|[az])([AZ])/\L\1-\2/g}'; ]]></content> <!-- Optional: Set a tabTrigger to define how to trigger the snippet --> <tabTrigger>import</tabTrigger> <scope>source.js</scope> </snippet> 
+1
source

Ugly ( substr() ), but works:

 $x = "MyFooBar"; $x =~ s/(?:^|(?<=[az])(?=[AZ]))(.)/-lc($1)/eg; $x = substr($x, 1); print $x; // my-foo-bar 

See a demo at ideone.com .

0
source

It took me a bit of time, but I believe that it does what you want, all within the same regex:

 use warnings; use strict; while (<DATA>){ chomp; s/([[:upper:]].*?)(?=(?:[[:upper:]]|$))/$+[0]!=length($_) ? lc($1).'-' : lc($1)/ge; print "$_\n"; } __DATA__ MyCamelCase AVeryLongCamelCaseStringWMXThat 

Output:

 my-camel-case a-very-long-camel-case-string-wmx-that 

Explanation: He searches for an uppercase letter and writes this letter in $1 along with all the other letters until he sees another capital or the end of the line in a zero-width image. on the substitution side, we use the /e modifier, which means that the right side is an expression (executable code). If the end of the last match ( $+[0] ) is less than the length of the string, we make a lower one ( lc() ) and add - . If the length of the string matches the end of the last match, we enter it in lowercase, and we are done.

0
source

Source: https://habr.com/ru/post/1241649/


All Articles