Menu Close

Regular expression allowing certain set of characters

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

Also with table of contents about symbols and their usage in Regex

you can also use regex to split string. Scroll down for more!!!πŸ‘‡ πŸ‘‡

Python makes regular expressions available through the re module.

If you want to check that a string contains only a certain set of characters, in this case a-z, A-Z and 0-9, you can do so like this,

import re
def is_allowed(string):
characherRegex = re.compile(r'[^a-zA-Z0-9.]')
string = characherRegex.search(string)
return not bool(string)
print (is_allowed("abyzABYZ0099"))
# Output: 'True'

print (is_allowed("#*@#$%^"))
# Output: 'False'

OUTPUT:
True
False


You can also adapt the expression line from [^a-zA-Z0-9.] to [^a-z0-9.] , to disallow uppercase letters for
example.

ILLUSTRATION

Executed using python3

Splitting a string using regular expressions

You can also use regular expressions to split a string. For example,

import re
data = re.split(r'\s+', 'James 94 Samantha 417 Scarlett 74')
print( data )

Output: [ ‘James’, ’94’, ‘Samantha’, ‘417’, ‘Scarlett’, ’74’ ]

ILLUSTRATION ON CLI

Executed using python3

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.

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


Morae Q!

  1. Program to get dictionary items, use update module and items module.
  2. Regular expression allowing certain set of characters.
  3. Compute the Sum of Cosine Series.
  4. Find the Sum of Sine Series.
  5. Remove the Vowels from the string.
  6. Find All Non-Overlapping strings/characters using regular expressions .
  7. Check whether the given string is Panagram or not.
  8. Replacing strings using Regular Expression Regex.
  9. Check whether the given number is Strong number or not.
  10. Find the numbers divisible by a input numbers within the given range.
  11. Change the behaviour of the Regular Expression using flags.
  12. Find the exponentiation of a number.
  13. Find all prime numbers in a range using Sieve of Eratosthenes.
  14. Convert decimal equivalent into binary, hexadecimal and octal.
  15. Compute the area of a farmers field in feet.
  16. Compute time from number of days/hours to seconds.
  17. Precompiled patterns using Regular Expressions .
  18. Compute the price of the cake on the nth day.
  19. Compute the number of possible non-increasing arrays.
  20. Find the type of spinach using the calorie value.