2018-10-06

Python Regex: how to find things between characters


How do you find a regex for all items between certain characters? (e.g. between [ ] or between ( )). Below is an example that finds anything between @ and .

use case: email.... using it on "hahah@trefis.com" will find "trefis"


-----------------------------

REGEX
(?<=@)(.*?)(?=\.)


---------------------------

SAMPLE SET
This is my first sentence. This is my second sentence.

haha@gmail.com

hello how are you

123@haha.co

This is my first sentence

----------------------------

EXPLANATION
(?<=@)TEST ....positive lookbehind. Find anything where "@" is just behind TEST 
(.*?) ....find all characters (.), one ore more (*), make it nongreedy so it stops at the first not last result (?)
(?=\.) .....positive lookahead. It must have a lookahead character that is a ".". backslash needed so activate the character itself 

--------------------------
https://regex101.com/r/eZ1gT7/1637
screenshot of regex in action
 

python - regex , from automate the boring stuff

automatetheboringstuff.com

Automate the Boring Stuff with Python



2018-09-15