RegEx in Sublime Text: Match any character, including newlines? - regex

Is there a way to match any character in Sublime Text, including newlines? I saw that Sublime uses Boost's syntax but that the . character won't match newlines without a specific flag set.

Try adding the (?s) inline flag start the start of the pattern. That will make . match any character.

Related

Find and replace using regular expressions - remove double spaces between letters only

Trying to do this in the Atom editor (1.39.1 x64, uBuntu 18.04), though assume this applies to other text editors using regular expressions.
Say we have this text:
This text has some double-spaces. Lets try to remove them.
But not after a full-stop or if three or more spaces.
Which we would like to change to:
This text has some double-spaces. Lets try to remove them.
But not after a full-stop or if three or more spaces.
Using Find with Regex enabled (.*), all occurrences are correctly found using: [a-zA-Z] [a-zA-Z]. But what goes in the Replace row to enforce the logic:
1st letter, single space, 2nd letter?
You can use this
([a-z])\s{2}([a-z])
and replace by $1 $2
Regex Demo
If your editor supports lookarounds you can use
(?<=[a-z])\s{2}(?=[a-z])
Replace by single space character
Regex demo
Note:- don't forget to use i flag for case insensitivity or just change the character class to [a-zA-Z]

Why won't this regex work in SublimeText when it does in Vim?

I have this regex
^\([^\t]*\)\t\([^\t]*\)\t\([^\t]*\)$
which is supposed to match
beginning of the line
a capture of all letter until a Tab
a capture of all letter until a Tab
a capture of all letter until a Tab
the EOL
In Vim, this works fine:
But in Sublime it will not match. Why?
Vim regex is rather specific and differs from the PCRE regex engine expression syntax that Sublime Text 3 uses.
In Sublime Text 3, you can write the pattern you used in Vim as
^([^\t\r\n]*)\t([^\t\r\n]*)\t([^\t\r\n]*)$
See the regex demo
In short, (...) should be used to form a capturing group, and you need to add \r\n to disallow a negated character class to match across lines (in Vim, [^.]* won't match a line break, but it will in Sublime Text 3).
Note that (...) (and not \(...\)) can also be used as a capturing group in Vim, but you need to use very magic mode to use that syntax.

Eclipse Add text to first line of all files

I need to add text to first line of all my JSP's in eclipse, this is the regex I a using \A.* but some how it selects the first line, I just want to prepend text to the start of the file. any help will be very much appreciated.
The .* pattern matches any 0+ chars other than line break characters, so it matches the first line.
It seems that Eclipse Find/Replace regex feature does not match entirely zero-width patterns (e.g. (?=,) will not find and insert a text before commas).
A workaround is to match and capture some text with (...) (where ... stand for a consuming pattern) capturing group and use $1 in the replacement pattern to reinsert the matched text.
Use
\A(.*)
Replace with MY_NEW_TEXT_HERE_AT_THE_START_OF_FILE$1.

Delphi regular expression, ignoring LineFeed in matched text

I want to search for the following regular expression
^[ ]*,$
in the following text :
,[LF]
,[LF]
My problem is that Delphi finds the expression, but the matched text doesn't include the LF.
Effectively I want to removes the lines from my source code.
I'am using TPerlRegEx with delphiXe8
In the example [LF] is the linefeed ($0D $0A)
I Tested several flags combinaisons in TPerlRegExOptions
This works perfectly in SublimeText 3
What am I missing ?
If you are using a PCRE regex, you can match zero or more spaces at the stsart of a line followed with a comma followed with a newline sequence with
(?m)^[ ]*,\R
See the regex demo. Note that (?m) is a multiline modifier making ^ match a location at the beginning of a line (after \n). \R matches any newline sequence.
Add a ? after \R to also match the last line in the text that has no newline sequence at the end.

regular expression in sublime text 2 to match text

I have a string that looks like this:
lonfksa.newsvine.com
and I have tons of file that looks like this:
http://ricambi.ru/avtomobilnie-novosti/lexus-gotovit-k-debiutu-obnovlenniy-rx
http://www.kiwibox.com/hoytboar/blog/entry/121424391/modis-tshirt-tips-untuk-womens-clothing/
http://www.euro-rockradio.com/archives/category/interview
http://lonfksa.newsvine.com/_news/2014/04/18/23538711-vampir-romantis-clothing
http://www.fam-hinterseer.de/cgi-bin/info.php?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Fwww.shopious.com%3Ecart+means+payment%3C%2Fa%3E
http://www.kiwibox.com/donniehihp/blog/entry/116146741/skin-care-beauty-makeup-tips-for-female/
http://www.kiwibox.com/karlagbr/blog/archive/2014/9/7/
I wanted to match the line that contains:
lonfksa.newsvine.com
and I tried the following regex but it doesn't work:
(?s)lonfksa.newsvine.com(?s)
what regex should I use to match the whole line that has this string?
You can make use of the multiline flag, and ^ and $ anchors that will match at the string start and string end repsectively:
(?m)^.*lonfksa\.newsvine\.com.*$
Mind that you need to escape a dot in regex to make it match a literal dot. Your regex (?s)lonfksa.newsvine.com(?s) contains unescaped dots that match any character (even a newline since you are using a singleline inline option (?s)). The final inline option (?s) is not necessary, it does not do anything.
Try this regex :
^.*lonfksa\.newsvine\.com.*\b
Demo