Question

In: Computer Science

Understanding RegEx to identify patterns of data. 1. Create 4 regular expressions to filter a specific...

Understanding RegEx to identify patterns of data.

1. Create 4 regular expressions to filter a specific data set.

2. In addition to the description, provide two test cases that will pass the input and one that will fail

Solutions

Expert Solution

A sample text string where regular expression   

1.>

Program : To search digit in string using regular expression in python

import re
string = """ Hello i am vikash , my age is 25 yrs and my area code is
90123 ans i like the number 890 along with this i live in
sector 345 kth block. """
  
# A sample regular expression to find digits.
regex = '\d+'   
  
match = re.findall(regex, string)
print(match)

OUTPUT:

['25', '90123', '890', '345']

2.> To check if word starts with "The" or not using regex

import re

string = "The time is very crucial for health in this pendamic"

#checking whether string starts with "the" or not

x = re.findall("\AThe",string)

print(x)

if x:
print("Yes, match is there")
else:
print("No match")

3.>To match alpha numeric characters in a given string we uses “^w+”:

import re
string= "vikash22 is my user id"
r1 = re.findall(r"^\w+",string)
print(r1)

# if we execute it will give result as vikash22 because it will only matches alphanumeric character (which contains alphabet or numbers) and matches with starting character , if matched found it will return . here ^ ensure that alphanumeric should be in starting of string.

output :

['vikash22']

4.> Regex for finding all capital letters character present in the string we uses [A-Z]+ it will gives ll the Capital letters that matches 1 or more times .


#This regex [A-Z]+ gives all Capital letters in the given string
import re
string= "VikaSh Is My90 User ID"
r1 = re.findall(r"[A-Z]+",string)
print(r1)

OUTPUT

['V', 'S', 'I', 'M', 'U', 'ID']

Concepts used in Regex :

Some important regex functions like

-> re.findall( ) : It will gives us the list of all matching patterns in the form of list as output

-> re.search( ) : It will search if the patterns matches using the given regex or not , if matches return True else return false.

-> re.match( ) : This function will search the regular expression pattern and return the first occurrence when it is found.



Related Solutions

Understanding RegEx to identify patterns of data. Create 10 regular expressions to filter a specific data...
Understanding RegEx to identify patterns of data. Create 10 regular expressions to filter a specific data set and explain what they each do.
RegEx (Regular Expressions) Make 8 regular expressions to filter a specific data set in Java and...
RegEx (Regular Expressions) Make 8 regular expressions to filter a specific data set in Java and explain what they do.
Regular expressions are used in Python for describing and identifying specific patterns. In Python, we use...
Regular expressions are used in Python for describing and identifying specific patterns. In Python, we use “re” or “Regex” to denote regular expressions. Write the Python Code to return matching analytics word in the following given text. Write the Python Code to return how many times analytics word is provided in this text. Definitions are useful to the extent they serve a purpose. So, is defining analytics important? Yes and no. It’s not likely that we’ll ever arrive at a...
linux: Regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter...
linux: Regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter the file so the output displays only the items requested. Put your full command in the space provided 1.Show only the lines that end with an ellipses (3 dots) :
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter...
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter the file so the output displays only the items requested. Put your full command in the space provided. 1. Display only the lines that were written to the file between the times of 12:55 and 12:59 (inclusive). This is tricky. Don’t think of these times as numbers, think of these times as a series of characters (a 1 followed-by a 2 followed-by a colon,...
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter...
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter the file so the output displays only the items requested. Put your full command in the space provided. 1. Display only the lines that were written to the file between the times of 12:55 and 12:59 (inclusive). This is tricky. Don’t think of these times as numbers, think of these times as a series of characters (a 1 followed-by a 2 followed-by a colon,...
Linux regular expressions: file name: lab3test.txt Create regular expressions that meet the following criteria. You may...
Linux regular expressions: file name: lab3test.txt Create regular expressions that meet the following criteria. You may use grep or egrep to answer these questions. 1.Match any lines that contain a phone number with the format 222-222-2222.
linux Regular expressions file name: lab3test.txt Create regular expressions that meet the following criteria. You may...
linux Regular expressions file name: lab3test.txt Create regular expressions that meet the following criteria. You may use grep or egrep to answer these questions. 1.Display all lines where the employee’s age is greater than 40 years old. Again, don’t think of this as a number, think of it as a series of characters.
submit a regular expression that will identify each of the following patterns. 1 - a US...
submit a regular expression that will identify each of the following patterns. 1 - a US telephone number that conforms to the following pattern 111.222.3456 2 - a US social security number that fits the following pattern 111-22-3456 3 - an American Express credit card number that fits the following format:       4 digits followed by a space followed by 6 digits followed by a space followed by 5 digits False positives are allowed in each case, so all you need...
Describe the languages specified by the following regular expressions: 1. \\(_+)/ 2. (\(740\)...-...)|(...-...) (The alphabet is...
Describe the languages specified by the following regular expressions: 1. \\(_+)/ 2. (\(740\)...-...)|(...-...) (The alphabet is {1,2,3,4,5,6,7,8,9,0})
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT