Question

In: Computer Science

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.

Solutions

Expert Solution

RegEx notes

========

A regular expression is a text string which contains a combination of some special characters called metacharacters and literals and is used to match ,search and replace text that follows a certain pattern .They are denoted as “regex” or “regexp” in short hand notation. Regular expressions are used in software tools as well as in programming languages.

The important metacharacters used in regex and their meanings are listed below,

/ - a backslash when used with another special cahracter indicates that ,the special cahracter needs to be treated as a literal .

[ ...] - When a certain set of characters are specified within square brackets any of the the characters can match the search string.For example, [0-9] indictes any number between 0 to 9 .

( ) - parenthesis is used to indicate the order of pattern evaluvation and replacement.

^ - usually indicates the beginning of a sentence.

[^...] - here the caret symbol is used to exclude or negate the following characters.

| - The alternation character or bar used to indicate “or” condition .Either of the strings separated by | will be used for matching.

* - The asterik symbol marks zero or more occuernces of characters to the left of the symbol.

? - character indictes zero or more occuernces of characters to the left of the symbol.

. - The dot character is used to match any single character

{} - this is used to limit repetitions by specifying minimaum and maximum number of repetitions as {min,max}

Examples of regular expressions

-------------------------------------------

1. Matching a word ,even it is misspelled.

For example consider the word “separate” , mostly the spelling mistake occures with ‘a’ and ‘e’ on either sides of letter ‘r’.The regular expression to match any misspelled word for this would be like

sep[ae]r[ae]te

2.Checking for an identifier in a programming language

An identifier is a name that contains alphabets,numbers and underscores but always starts with an alphabet or an underscore.The regex to match this pattern is given below,

[A-Za-z_][A-Za-z_0-9]*

3.Matching HTML tags.

The start and end html tags usually looks like <TAG></TAG>.The regex to match this pattern without considering nested tags is ,

<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1>

4. Ip address matching

The below regex will strictly match all the numbers in the ip adress to 0 ...255 and may disallow any leading zeroes.

\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.
(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.
(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.
(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b

5.Matching floating point numbers

[-+]?[0-9]*\.?[0-9]+

6.Validating email address

^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$



7.Matching a valid date 

To match a date in mm/dd/yyyy format ,the following regex can be used.

(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d



8. Master card numbers

mastercard numbers either begins with 51-55 or with 2221-2720 and contains 16 digits .this can be matched using the following regex

^(?: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}$




9.Matchingg entire line containg a word

Here the whole line containing the word  example will be matched.

«^.*example.*$». 



10.C-style hexadecimal number
regex matching c-style hexadecimal number is like,

\b0[xX][0-9a-fA-F]+\b





Related Solutions

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
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...
The code to create a Search/Filter Data with Javascript or html from html page.
The code to create a Search/Filter Data with Javascript or html from html page.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT