<jupyter_start><jupyter_text>Extract IP Address using regex<jupyter_code>import re
ip = re.compile('(([2][5][0-5]\.)|([2][0-4][0-9]\.)|([0-1]?[0-9]?[0-9]\.)){3}'
                +'(([2][5][0-5])|([2][0-4][0-9])|([0-1]?[0-9]?[0-9]))')

match = ip.search("Your ip address is 192.168.0.1, have fun! and 192.168.0.2")
if match:
 print ('IP address found:'),
 print (match.group()) # matching substring
 print ('at position',match.span()) # indexes of the substring found
else:
 print ('IP address not found')
import re
ip = re.compile('(((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))\.){3}(((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2})))')

matches = ip.findall("Your ip address is 192.168.0.1, have fun! and 192.168.0.2")
for m in matches:
    print(m)
## Suppose we have a text with many email addresses
str = 'purple <EMAIL>, blah monkey <EMAIL> blah dishwasher'

  ## Here re.findall() returns a list of all the found email strings
emails = re.findall(r'[\w\.-]+@[\w\.-]+', str) ## ['<EMAIL>', '<EMAIL>']
for email in emails:
    # do something with each found email string
    print (email)
import re
ip = re.compile('\w+ly')
text="He was carefully disguised but captured quickly by police."
for m in ip.finditer(text):
    print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0)))
import re
ip = re.compile('((((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))\.){3}(((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))))')
text="Your ip address is 192.168.0.1, have fun! and 192.168.0.2"
for m in ip.finditer(text):
    print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0)))
import re
ip = re.compile('((((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))\.){3}(((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))))')

def matchips(text):
    for m in ip.finditer(text):
        print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0)))

text="Your ip address is 192.168.0.1, have fun! and 192.168.0.2"
matchips(text)
import re
ip = re.compile('((((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))\.){3}(((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))))')

class RegexMatch:

  def __init__(self):
    self.start = 0
    self.end = 0
    self.matchingtext = ''
    
  def __init__(self, start, end, matchingtext):
    self.start = start
    self.end = end
    self.matchingtext = matchingtext
    
def get_matches(text, compiledregex):
    matches = []
    for m in compiledregex.finditer(text):
        ipmatch = RegexMatch(m.start(), m.end(), m.group(0))
        #ipmatch.start = m.start()
       # ipmatch.end = m.end()
       # ipmatch.matchingtext = m.group(0)
        matches.append(ipmatch)
        #print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0)))
    return matches
text="Your ip address is 192.168.0.1, have fun! and 192.168.0.2 0.0.0.0"
allmatches = get_matches(text,ip)
for a in allmatches:
    print('%02d-%02d: %s' % (a.start, a.end, a.matchingtext))


# this is correction to https://stackoverflow.com/a/39136481
# Following is python
#usable ips
import re

ip_list = ['10.101.0.11','011.101.232.111', '01.01.01.01','000.000.000.000','00.00.00.00','0.0.0.0','011.022.022.011']
ipv4_regex = r'^(25[0-5]|2[0-4][0-9]|[1-2]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[1-2]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[1-2]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[1-2]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$'
usable_ip = re.compile(ipv4_regex)

for i in ip_list:
    resp = re.match(ipv4_regex, i)
    print ('match', i) if resp else print("not match", i)
    

#extract email
import re 

pattern = '[\w\.-]+@[\w\.-]+'
#pattern = '\S+@\S+'
# \S matches any non-whitespace character
# @ for as in the Email
# + for Repeats a character one or more times
email = re.compile(pattern)
 
# Example string 
text = 'Hello from <EMAIL> to <EMAIL> about the meeting @2PM also call <EMAIL>'

allmatches = get_matches(text,email)
for a in allmatches:
    print('%02d-%02d: %s' % (a.start, a.end, a.matchingtext))
pattern = "\d{9}|\d{3}-\d{2}-\d{4}"
ssn = re.compile(pattern)

text = 'Hello Here are the SSNs to hack 428-34-4566, 428-34-4566 , 428-34-4545'
allmatches = get_matches(text,ssn)
for a in allmatches:
    print('%02d-%02d: %s' % (a.start, a.end, a.matchingtext))
pattern = "((ht|f)tp(s?)\:\/\/|~/|/)?([\w]([\w\-]+\.)+([\w]{2,63}))(:[\d]{1,5})?"
url = re.compile(pattern)

text = 'Hello Here are the URLs to hack http://www.abc.com, http://abc.edu http://www.123abc.io'
allmatches = get_matches(text,url)
for a in allmatches:
    print('%02d-%02d: %s' % (a.start, a.end, a.matchingtext))
pattern = "s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*"
ipv6 = re.compile(pattern)

text = 'Hello Here are the IPV6 to hack FE80:0000:0000:0000:0202:B3FF:FE1E:8329, FE80::0202:B3FF:FE1E:8329 [2001:db8:0:1]:80 [2001:db8::1]:80 http://[2001:db8:0:1]:80'
allmatches = get_matches(text,ipv6)
for a in allmatches:
    print('%02d-%02d: %s' % (a.start, a.end, a.matchingtext))<jupyter_output>32-71: FE80:0000:0000:0000:0202:B3FF:FE1E:8329
73-98: FE80::0202:B3FF:FE1E:8329
118-129: 2001:db8::1<jupyter_text>Credit Cards<jupyter_code>pattern = '4[0-9]{12}(?:[0-9]{3})?'
visa = re.compile(pattern)

text = 'Hello Here are the credit cards to hack 4242424242424242 4003600000000006 4003600000000014 4900000000000086 4900000000000003 5100000000000016'
allmatches = get_matches(text,visa)
for a in allmatches:
    print('%02d-%02d: %s' % (a.start, a.end, a.matchingtext))
pattern = '3[47][0-9]{13}'
amex = re.compile(pattern)

text = 'Hello Here are the credit cards to hack 375811111111115 375000000000007 375811111111123 36168002586009 36167719110012 5100000000000016'
allmatches = get_matches(text,amex)
for a in allmatches:
    print('%02d-%02d: %s' % (a.start, a.end, a.matchingtext))
pattern = '(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}'
mastercard = re.compile(pattern)
text = 'Hello Here are the credit cards to hack 5100000000000008 5404000000000001 5200000000000007375811111111115 5200000000000080 5100000000000016 375811111111123 36168002586009 36167719110012 5100000000000016'
allmatches = get_matches(text,mastercard)
for a in allmatches:
    print('%02d-%02d: %s' % (a.start, a.end, a.matchingtext))
pattern = '3(?:0[0-5]|[68][0-9])[0-9]{11}'
dinersclub = re.compile(pattern)
text = 'Hello Here are the credit cards to hack 36168002586009 36167719110012 361677191100125100000000000008 5404000000000001 5200000000000007375811111111115 5200000000000080 5100000000000016 375811111111123 36168002586009 36167719110012 5100000000000016'
allmatches = get_matches(text,dinersclub)
for a in allmatches:
    print('%02d-%02d: %s' % (a.start, a.end, a.matchingtext))
pattern = '6(?:011|5[0-9]{2})[0-9]{12}'
discover = re.compile(pattern)
text = 'Hello Here are the credit cards to hack 3569990010030442 3569990010030400 6011000000000004 6011000000012345 6011000123000004 36168002586009 36167719110012 361677191100125100000000000008 5404000000000001 5200000000000007375811111111115 5200000000000080 5100000000000016 375811111111123 36168002586009 36167719110012 5100000000000016'
allmatches = get_matches(text,discover)
for a in allmatches:
    print('%02d-%02d: %s' % (a.start, a.end, a.matchingtext))
pattern = '(?:2131|1800|35\d{3})\d{11}'
jcb = re.compile(pattern)
text = 'Hello Here are the credit cards to hack 3569990010030442 3569990010030400 6011000000000004 6011000000012345 6011000123000004 36168002586009 36167719110012 361677191100125100000000000008 5404000000000001 5200000000000007375811111111115 5200000000000080 5100000000000016 375811111111123 36168002586009 36167719110012 5100000000000016'
allmatches = get_matches(text,jcb)
for a in allmatches:
    print('%02d-%02d: %s' % (a.start, a.end, a.matchingtext))<jupyter_output>40-56: 3569990010030442
57-73: 3569990010030400<jupyter_text>Lat Long<jupyter_code>pattern='\(([^,]*),([^\)]*)\)'

latlong = re.compile(pattern)
text = 'Hello Here are the latlong locations to hack (51.498134,-0.201755) (+90.0, -127.554334) 47.1231231, -90., -180. +90.1, -100.111 -91, 123.456 179.99999999 -90, -180 -90.000, -180.0000 47.1231231, 179.99999999'
allmatches = get_matches(text,latlong)
for a in allmatches:
    print('%02d-%02d: %s' % (a.start, a.end, a.matchingtext))
#!pip install regex
import regex

commlines = 'Hello Here are the latlong locations to hack (51.498134,-0.201755) (+90.0, -127.554334) 47.1231231, -90., -180. +90.1, -100.111 -91, 123.456 179.99999999 -90, -180 -90.000, -180.0000 47.1231231, 179.99999999'

latlong = regex.compile(r"\((?<lat>[-\d.]+), ?(?<long>[-\d.]+)\)", regex.VERBOSE)

result = regex.findall(latlong, commlines)

template = "{0:15}|{1:10}" # column widths
print(template.format("lat", "long")) # header

for t in result:
    print('{}       {}'.format( t[0], t[1]))<jupyter_output>lat            |long      
51.498134       -0.201755<jupyter_text>US Phone Numbers<jupyter_code>pattern='\(?\d{3}\)?[-\.]? *\d{3}[-\.]? *[-\.]?\d{4}'
usphone = re.compile(pattern)
text = 'Hello Here are the latlong locations to hack 9164445678 965-383-2838 1231231238 12-234-2342 123-12-1231 234-234-2345'
allmatches = get_matches(text,usphone)
for a in allmatches:
    print('%02d-%02d: %s' % (a.start, a.end, a.matchingtext))<jupyter_output>45-55: 9164445678
56-68: 965-383-2838
69-79: 1231231238
104-116: 234-234-2345<jupyter_text>International Phone Numbers<jupyter_code>pattern='\s*\+?[0-9]\d?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}'

intlphone = re.compile(pattern)
text = 'Hello Here are the latlong locations to hack 011912224781213 9164445678 965-383-2838 1231231238 12-234-2342 123-12-1231 234-234-2345'
allmatches = get_matches(text,intlphone)
for a in allmatches:
    print('%02d-%02d: %s' % (a.start, a.end, a.matchingtext))<jupyter_output>46-58: 119122247812
58-71: 13 9164445678
80-92: 2838 1231231
117-132: 31 234-234-2345
{'fraction_non_alphanumeric': 0.246993006993007, 'fraction_numerical': 0.2413986013986014, 'mean_word_length': 4.886937431394072, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 18, 'substrings_counts': 49, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '73483', 'license': 'Apache-2.0', 'n_tokens_mistral': 6375, 'n_tokens_neox': 4857, 'n_words': 812, 'path': 'Extract Entities from Data with Regex.ipynb', 'repo_name': 'dynamicdeploy/datasec', 'size': '10791'}