Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
URL Regex in python:
import re
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)
#python #regex #re #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 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 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 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