Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textmate snippet to format string (regex?)

I'm trying to create a textmate snippet that will transform this:

HELLO WORLD<br />
SAY ANYTHING

To this:

hello world say anything

Any help?

like image 752
maček Avatar asked Dec 01 '25 21:12

maček


1 Answers

With a little help from Allan Odgaard himself:

snippet

begin

  in:  ${1:Hello}

  out: ${1/\A\s+|\s+\Z|\s*\n\s*|(\<br\s*\/?\>)|(.)/(?1: :\L$2)/ig}

end

test

begin

  in:  THIS IS<br />
  WORKING<BR>
  VERY NICELY<br/>

  EVEN WITH MULTIPLE<BR />
  LINE BREAKS!

  out: this is working very nicely even with multiple line breaks!

end

Regex Match Explanation

\A      beginning of buffer
\s+     followed by one or more whitespace

|       OR

\s+     one or more whitespace
\Z      followed by end of buffer

|       OR

\s*     zero or more whitespace
\n      followed by newline
\s*     followed by zero or more whitespace

|       OR

(       Capture to 1
  <br   Literal `<br'
  \s*   followed by zero or more whitespace
  \/?   followed by one or zero literal `/'
  >     followed by literal `>'
)       End capture

|       OR

(       Capture to 2
  .     Any character
)       End capture

Regex Replace Explanation

note: conditional replacements are unique to TextMate

(?1:    IF Capture 1 is found
        Insert space (' ')
  :     ELSE
  \L$2  Insert lowercase(Capture 2)
)       ENDIF

Regex Modifiers Explaination

i       case insensitive
g       global match/replace
like image 157
maček Avatar answered Dec 04 '25 14:12

maček



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!