In: Computer Science
Regarding Python.
1.
How would you write a regex that matches the hour of the day from the extracted line shown below? (The highlighted portion)
From [email protected] Sat Jan 5 09:14:16 2008
Question options:
ˆFrom .+ [0-9] ˆX-.*: [0-9.]+ [0-9]: |
|
ˆFrom .* [0-9][0-9]: |
|
ˆFrom .* ('ˆX\S*: ([0-9.]+)', line) |
A _____________ sign at the end of the regular expression indicates that the string must end with the specified regex pattern.
Question options:
question (?) |
|||
caret (^) |
|||
dollar ($) |
|||
0 / 1 point |
|||
Which of the following will be a correct match for the regex "F..M"
Question options:
F!@M |
|
FRONM |
|
FOR |
Which of the following is used by Unix grep to match a character that is anything other than a space?
Question options:
"/S" |
|
"[ˆ ]" |
|
"/s" |
Python uses a ___________algorithm to search for keys in a dictionary?
Question options:
hash table |
|
reference list |
|
search engine |
_____________ is a special character that matches any character.
Question options:
Escape character |
|
Wild card |
|
Regular expression |
import re
hand = open('mbox-short.txt')
for line in hand:
line = line.rstrip()
if re.search('ˆFrom:.+@', line) :
print line
What is the correct interpretation of the highlighted portion of the script shown above?
Question options:
The search will match lines that end with "From:", followed by an @ sign, followed by zero or more characters. |
|
The search will match lines that start with "From:", followed by one or more characters, followed by an @ sign. |
|
The search will match lines that end with "From:", followed by a plus (+) sign, followed by an @ sign. |
Question1
The correct option is C) ˆFrom .* ('ˆX\S*: ([0-9.]+)', line)
,
This is because we need the second parameter which is the strign on
which regex is to be applied , also we nned to seacrhc only the
hour which is the first two digits and hence the given option
Question 2
The corretc option si option C) dollar ($) ,
caret sign is used to match startign characters and ? is used to
match a single character , $ is ised to match ending characters
Question 3
The correct option is option a F!@M
. menas any character except line break so the qiven regex will match any word having 4 characters and so the first option
Question 4
The correct option will be option b[^ ]
^ enclosed in [] means not to include anything mentioned inside the
barcket , in this case the space.
Question 5
The correct option i a) hash table
python uses hash table algoritm to search for keys in a dictionary
Question 6
The correct option is b) wild card
In regex wild cards are used to match any character
Question 7
The correct option is b) The search will match lines that start with "From:", followed by one or more characters, followed by an @ sign.
^ meatches the start of strign and .+ means 0 or more characters so option b