Regular expressions for all occasions

LightWood

Moderator
Регистрация
04.11.2010
Сообщения
2 382
Благодарностей
915
Баллы
113
We collect the people's list of regulars (regex) for all occasions, I'll start first. Who has their own - throw, add to the first post indicating the author. Regulars can sometimes work incorrectly, if that write off in this thread - we will find a solution.

List of regular expressions and typical usage contexts
Search for a number greater than 0
Код:
(?<=(\D|^))[1-9]\d*
Use case:
Снимок экрана от 2017-12-03 17-09-18.png


In this example, the visible elements are searched for only, the invisible elements (having a width of 0px) are ignored, but sometimes there are misfires - when a site overlays elements (it is found in Google for example).

Thanks: CSS, ZennoScript
Parsing the text for sentences, seems author is ZennoScript, I have slightly modified
Код:
[А-ЯA-Z].{15,}?(\.|\!|\?)(?=\ |\r|\n|$)
work example, initial data:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce dapibus tellus nec nulla gravida, sed congue nunc hendrerit. Integer interdum elementum lorem id rutrum. Ut sit amet interdum mauris. Phasellus mollis ex eleifend lacus molestie dictum. Fusce blandit, ligula non condimentum maximus, massa nisi ullamcorper odio, et vehicula nisl nunc nec orci. Sed neque diam, gravida eu blandit ullamcorper, porttitor non lorem. Etiam sagittis diam a dolor feugiat placerat. Suspendisse enim turpis, imperdiet in tellus sit amet, consectetur porta magna. Suspendisse odio nulla, imperdiet eget augue in, pulvinar hendrerit nunc. Aenean ut cursus tellus, nec vehicula ante. Vestibulum ornare erat non ante tempus, eu aliquet felis dapibus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Mauris eget arcu imperdiet, laoreet erat non, imperdiet leo. Sed arcu mi, ornare non leo sed, faucibus semper nunc.
result:
Снимок экрана от 2017-12-03 17-12-23.png


Thanks: ZennoScript
Search for a word starting with a specific text (Thank ZennoScript for the modification)
Код:
(?i)\bgo.*?(?=\W|\ |\r|\n|$)
Find in the text all the words beginning with go, for example: goal, god, google

Thanks: CSS, ZennoScript
Searches for a number in the range 400-699:
Код:
^([4-6][0-9][0-9])$
more about the range you can see here http://www.regular-expressions.info/numericranges.html

Shared: CSS
Virtual columns with separator ";"
Код:
(?<=(^|;)).*?(?=(;|$|\r\n))
initial data: name;pass;mail
result: the "column" you need by the match number:
Посмотреть вложение 8857
use case: we work with the list, we disassemble by the pieces of the column as in the table

Shares: CSS
Get the file name from the windows path:
Код:
([^\\]+$)
initial data:
С:\Program Files (x86)\ZennoLab\ZennoPoster Pro\Progs\ZennoPoster.exe
result: ZennoPoster.exe

Shared: CSS
Search for strings NOT containing the specified (sub) string:
Код:
(?<=(^|\r\n))((?!(test)).)*(?=($|\r\n))
initial data:
result:
Shared: CSS
Take all the lines of more than 18 characters
Код:
(?<=(\r\n|^)).{18,}(?=(\r\n|$))
Thanks: alekwuy
Get all lines less than 18 characters
Код:
(?<=(\r\n|^)).{1,18}(?=(\r\n|$))
Thanks: alekwuy
Search all HTML tags
Код:
<.*?>
Use case - clear text from HTML code

Thanks: Trader1985
Find all empty lines in the text
Код:
\n\r
Use case - text correction

Thanks: Trader1985
Search from three or more blank lines
Код:
(?<=(\r\n){2})(\r\n){1,}
Use case - text correction if we are satisfied with 2 blank lines

Thanks: Trader1985
Email:
Код:
[\.\-_A-Za-z0-9]+?@[\.\-A-Za-z0-9]+?[\.A-Za-z0-9]{2,}
IP
Код:
(\d{1,3}\.){3}\d{1,3}
URL
Код:
(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?
IP:PORT (for example for parsing a proxy)
Код:
(\d{1,3}\.){3}\d{1,3}:\d*
Thanks: up_lvl
[TBODY] [/TBODY]


Origin: Link
Author: CSS
Translate by: LightWood
 

ahweb

Новичок
Регистрация
03.12.2013
Сообщения
1
Благодарностей
1
Баллы
3
Very helpful. Thanks.
 
  • Спасибо
Реакции: LightWood

LightWood

Moderator
Регистрация
04.11.2010
Сообщения
2 382
Благодарностей
915
Баллы
113
Регистрация
10.07.2017
Сообщения
29
Благодарностей
1
Баллы
3
Really helpful for a newbie like me :-)
 

Smasher

Новичок
Регистрация
12.06.2016
Сообщения
7
Благодарностей
0
Баллы
1

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)