Is it possible to use nothing but Regular Expressions to convert strings like "hello_world" into "HelloWorld" and back?
I'm asking because I often need to create snippets for Sublime Text that automatically fills in "class_name" somewhere when I type "ClassName" somewhere else. I can only use perl-style regular expressions for this purpose.
The _ (underscore) character in the regular expression means that the zone name must have an underscore immediately following the alphanumeric string matched by the preceding brackets. The . (period) matches any character (a wildcard).
Just escape the dashes to prevent them from being interpreted (I don't think underscore needs escaping, but it can't hurt). You don't say which regex you are using.
Regular expressions are used with the RegExp methods test() and exec() and with the String methods match() , replace() , search() , and split() .
In formal language theory, a regular expression (a.k.a. regex, regexp, or r.e.), is a string that represents a regular (type-3) language. Huh?? Okay, in many programming languages, a regular expression is a pattern that matches strings or pieces of strings.
Using perl regular expression:
hello_world -> HelloWorld
s/(_|\b)([a-z])/\u\2/g;
\b
: Match at boundary (space, start of string, punctuation mark, ..)[a-z]
: lowercase alphabet\u
: make uppercase for next character\2
: group 2 (first lowercase character)(_|\b)
-> group 1([a-z])
-> group2HelloWorld -> hello_world
s/([A-Z][a-z]+|[a-z]+)([A-Z])/\l\1_\l\2/g;
Hello
.s/([A-Z])/_\l\1/g;
followed by s/^_//;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With