URL Regex in python:
import re#python #regex #re #url
url_regex = re.compile(
r'^(?:http)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
r'localhost|' # localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
is_valid_url = url_regex.match(input_url)
sed
linux command:sed
stands for Stream Editor
. As its name advertise it works on stream of text (input file, input pipleline).General command executation:
sed REGEX INPUTFILE
For instance if you want to replace
hi
with bye
in hibye.txt file:sed 's/hi/bye/' hibye.txt
It will print the result into standard output (console). If you want to put the output in a file:
sed 's/hi/bye/' hibye.txt > output.txt
Instead of
INPUTFILE
we can put -
which then reads from standard input:cat hibye.txt | sed 's/hi/bye/' -
Now if you want to put the output in a file:
cat hibye.txt | sed 's/hi/bye/' - > output.txt
As until now you have seen,
sed
command writes output to standard output, if you want to write the result to the file itself, use -i
as below:sed -i 's/hi/bye' hibye.txt
So in the above command the result will be written to
hibye.txt
itself.#linux #sed #regex
How to use regex in
I had a query that I had to find records that their numbers does not start with
The above regex says that the number should not start with
#mongodb #mongo #regex
MongoDB
?I had a query that I had to find records that their numbers does not start with
+98
. For regex you need to use $regex
in MongoDB find query:db.my_collection.find({'destination_number': {'$regex': '^(?!\\+98)(\\+.*).*$'}})
The above regex says that the number should not start with
+98
and then it says that it should start with +
(to ignore non-standard numbers, strings, etc).NOTE:
?!
is used to negate the regex inside of parenthesis.#mongodb #mongo #regex
In
I wanted to add
It will find all lines which has different names: .* and put that in a variable. (you can put something you have found in a variable by using parenthesis).
Now we can access the variable using
The above code will put method name using $1 and the append
#pycharm #regex #find #replace
Pycharm
I wrote something like below in multiple lines in the file:method_name='get_account'
I wanted to add
_v2
to all the method names, what I did was to use regex
in PyCharm replace functionality. Press Command+R
in order to open replace dialog. In the dialog there is an option called Regex
, tick the checkbox in front of it and in find section write:method_name='(.*)'
It will find all lines which has different names: .* and put that in a variable. (you can put something you have found in a variable by using parenthesis).
Now we can access the variable using
$1
. We now need to put the below code in replace section:method_name='$1_v2'
The above code will put method name using $1 and the append
_v2
to all the methods.#pycharm #regex #find #replace
In
It will find all users that their names start with
#mongoDB #pymongo #regex
MongoDB
you can use $regex
in order to find something based on a regex pattern:my_col.find({'name': { $regex: '^ali.*' } })
It will find all users that their names start with
ali
. Now let's say you want to search users based on their phone country code which has a + in its number like +98901...
. You need to escape the + character but escape it twice:my_col.find({'phone': { $regex: '^\\+98.*' } })
#mongoDB #pymongo #regex
https://www.digitalocean.com/community/tutorials/using-grep-regular-expressions-to-search-for-text-patterns-in-linux
#linux #grep #regex
#linux #grep #regex
Digitalocean
Mastering grep with Regular Expressions: Guide for Efficient Text Search | DigitalOcean
Learn how to use grep with regular expressions to search, filter, and process text in Linux and Unix systems. This guide covers syntax, practical use cases, …