Menu Close

Splitting strings and substituting using regular expressions regex

Basics of Regular Expressions in Python, check the link below πŸ‘‡ πŸ‘‡
Regular Expressions Regex basic in Python πŸ‘ˆ πŸ‘ˆ

Splitting a String

The split() method breaks a string at each occurrence of a certain pattern.
Consider the following line from the any file,
line = “OverLord:legacyboy:fcukthecode/home/:/bin/bash”

We can use split() to turn the fields from this line into a list,

import re
line = "OverLord:legacyboy:fcukthecode/home/:/bin/bash"
passre = re.compile(r":")
passlist = passre.split(line)
#passlist = ['OverLord', 'legacyboy', 'fcukthecode/home/', '/bin/bash']

# Better yet, we can assign a variable name to each field.
(Logname,Username, Home, Shell) = re.split (r":", line)

print(Logname,Username, Home, Shell)
#Output:  OverLord legacyboy fcukthecode/home/ /bin/bash

ILLUSTRATION USING PYTHON 3 CLI

Substitutions

Regular expressions can also be used to substitute text for the part of the string matched by the
pattern. In this example, the string β€œOver, fcukthecode.com” is transformed into β€œLord, fcukthecode.com”:

import re
temp = "Over, fcukthecode.com"
tempA = re.compile(r"Over")
tempB = tempA.sub("Lord", temp)
print(tempB) # Output:  Lord, fcukthecode.com


#This could also be written as
anime = "Over, fcukthecode.com"
newanime = re.sub (r"Over", "Lord", anime)
print(newanime) #Output:  Lord, fcukthecode.com

ILLUSTRATION USING CLI

Executed using python 3

Here’s a slightly more interesting example of a substitution. This will replace all the digits in the input
with the letter X:

import re
Xdigit = re.compile(r"\d")
temp = input("String: ")

print(Xdigit.sub("X",temp)) # This will replace all the digits in the input
    

INPUT:
String:  101fcukthecode101

OUTPUT:
XXXfcukthecodeXXX



Other useful things to do using regular expressions in python. links here down : )

For the first part in using regular expressions and matching the string
you can visit this link –>   πŸ‘‡ πŸ‘‡
Matching the beginning of a string (Regex) Regular Expressions in python

The re.search() method takes a regular expression pattern and a string and searches for that pattern within the string. If the search is successful, search() returns a match object or None otherwise.
For the second part in using regular expression and searching the string
visit –> Searching – Regular Expressions (Regex) in Python

Replacing Strings using Regex Python
Replacements can be made on strings using re.sub. πŸ‘‡ πŸ‘‡ πŸ‘‡
Replacing Strings using Regular Expression Regex in Python..-FcukTheCode.com
Replacing strings and Using group references Replacements with a small number of groups can be made.


Morae Q!

  1. OS operating system module using path parameter.
  2. Find the smallest and largest integers after sorting using bubble sort.
  3. Find the integer and its number of occurrences.
  4. Algorithm complexity – Big O Notation with Examples.
  5. Linear search.
  6. Map module using series mapping and parallel mapping.
  7. Mapping and Transposing with Map Module.
  8. Map Module/built-in Function.
  9. Linux commands for managing files.
  10. Program which takes two lists and maps two lists into a dictionary.
  11. Splitting strings and substituting using regular expressions regex.
  12. Basics of regular expression Regex.
  13. Find the square value of the dictionary.
  14. Check whether the given key is present in the dictionary.
  15. Matching an expression only in specific places using regular expressions.
  16. Escaping special characters using regular expressions.
  17. Check the key.
  18. Grouping and sub-grouping using regular expressions.
  19. Generate a Dictionary that Contains Numbers in the Form (x , x*x).
  20. Algorithm complexity Big-Theta, Big-Omega and Big-O Notations.