# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# <a href="https://apssdc.in"><img src="https://camo.githubusercontent.com/e7501c5948d48f88dad8ab2ab6bd448e1cfd6c79/68747470733a2f2f64726976652e676f6f676c652e636f6d2f75633f6578706f72743d646f776e6c6f61642669643d3135414b51365f2d42697857344b366d4c36525070684635454b58715946327a6a" width="900" align="center"></a>
#
# <h1><center>Day12 Python Programming</center></h1>

# ### Day Objectives
#
# - File Handling Contd..
# - Regular Expressions

# File Modes
# 1. r
# - w
# - a
# - r+
# - w+
# - a+
# - wb
# - rb
# - ab
# - wb+
# - wa+
# - wr+

with open('regex.txt', 'r') as f:
    print(f)
    #data = f.read()
    #print(data)

    print(f.readlines())

# ### a+, w+, r+

with open('../regex.txt', 'r+') as f:
    data = '\nNew data appended using r+ mode in the last line'
    f.write(data)
    data = f.readlines()[-1]
    print(data)

# ### Copying data from one file to other files
#
#
# - Open the Original File in `r` mode
# - open create the req copy file in `w`
# - do some operation
# - Close the opened files

with open('../regex.txt', 'r') as f:
    with open('copy1.txt', 'w') as cf1:
        with open('copy2.txt', 'w') as cf2:
            read_data = f.read()
            cf1.write(read_data)
            cf2.write(read_data)

# ### Copying data from one file to 10 different files

with open('../regex.txt', 'r') as f:
    read_data = f.read()
    for num in range(1,11):
        with open('fileCopy{}.txt'.format(num), 'w') as cf:
            cf.write(read_data)

with open('../regex.txt', 'r') as f:
    read_data = f.read()
    for num in range(1,11):
        with open('fileCopy{}.doc'.format(num), 'wb') as cf:
            cf.write(read_data.encode())

# ### Creating a file with rollNumbers from 18X41A1201 to 18X41A1299

# +
# File Name RollNumber.txt

with open('rollNumber.txt', 'w') as f:
    s = '18X41A12'
    s1 = s + '0'
    for i in range(1, 100):
        if i < 10:
            rollNum = s1 + str(i)
        else:
            rollNum = s + str(i)
        f.write(rollNum + '\n')
# -

# ## Regular Expression
#
# It is a sequence of characters that define a search pattern.
#
#
# - `re` is the package avaliable in python for implementing the Regular EXpressions.
# - Our Data Should avaliable in String format
#
#
# #### Syntax 
#
# method(pattern, string)

import re

# ### Search

s = 'APSSDC Python Online Training for SRKIT Students'

print(re.search('AP', s))

print(re.search('in', s))

print(re.search('apssdc', s))

print(re.search('AC', s))

# +
roll = '18X41A1299'

re.search('18X41A12', roll)
# -

# ### Match()

re.match('APSSDC', s)

print(re.match('18', s))

print(re.match('PSSDC', s))

# ### findall()

re.findall('in', s)

print(re.findall('ap', s))

# ### sub() - Used to replace the data in the main String
#
#
# #### Syntax:
#
# re.sub(pattern, newString, OriginalString)

re.sub('in', 'IN', s)

s

re.sub('SRKIt', 'IN', s)

# ### Split

s2 = """A regular expression (shortened as regex or regexp;[1] also referred to as rational expression[2][3]) is a sequence of characters that define a search pattern. Usually such patterns are used by string-searching algorithms for "find" or "find and replace55" operations on strings, or for input validation. It is a techn7ique developed in theoretical computer s8cience and formal 5language theory."""

s2

re.split('\. ', s2)

# ### Special characters for creating `re` patterns 
#
# 1. `^` - Checking the string is started with that pattern
# 2. `$` - Checking the string is ended with that pattern
# 3. `*` - any characters
# 4. `\d` - for matching the digits
# 5. `\w` - for matching alphabets
# 6. `\s` - for matching space

re.findall('\d', s2)

print(re.findall('\w', s2))

print(re.findall('\s', s2))

print(re.findall('^r', s))

print(re.findall('\D', s2))

print(re.sub('\s', '-', s2))

StarcoderdataJupyterScriptsDedupFiltered