Please answer the quetion : Question 43 : Which of the following regular express
ID: 657349 • Letter: P
Question
Please answer the quetion :
Question 43 : Which of the following regular expressions only matches variable names with all of the following rules?
1) Variable names must start with an alphabetic character or an underscore.
2) If a variable name starts with an underscore, the variable name must include at least one alphabetic character.
3) Variable names my include numbers at any position after the start.
Note: This question has one of the lowest correct answer rates of any question used in CEG2350 exams. Why is this? If the answer isn't obvious to you just by looking at the options, why don't you create some test cases and actually test each of the candidate regular expressions?
Question 43 options:
(^[a-zA-Z][a-zA-Z0-9_]*)|(^[_]+[a-zA-Z0-9_]*)
(.[a-zA-Z][a-zA-Z0-9_]*)|(^[_]*[a-zA-Z0-9_]$)
(^[a-zA-Z][a-zA-Z0-9_]*)|(^[_]+[0-9]*[a-zA-Z]+[a-zA-Z0-9_]*)
(.[a-zA-Z][a-zA-Z0-9_]*)|(^[_]+[a-zA-Z0-9_]*)
(^[a-zA-Z][a-zA-Z0-9_]*)|(^[_]+[a-zA-Z0-9_]*)
(.[a-zA-Z][a-zA-Z0-9_]*)|(^[_]*[a-zA-Z0-9_]$)
(^[a-zA-Z][a-zA-Z0-9_]*)|(^[_]+[0-9]*[a-zA-Z]+[a-zA-Z0-9_]*)
(.[a-zA-Z][a-zA-Z0-9_]*)|(^[_]+[a-zA-Z0-9_]*)
Explanation / Answer
Ok, So first I will provide the full explanations of all regular explession here:
1.
(^[a-zA-Z][a-zA-Z0-9_]*)|(^[_]+[a-zA-Z0-9_]*)
1st Alternative: (^[a-zA-Z][a-zA-Z0-9_]*)
1st Capturing group (^[a-zA-Z][a-zA-Z0-9_]*)
^ assert position at start of the string
[a-zA-Z] match a single character present in the list below
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
[a-zA-Z0-9_]* match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
0-9 a single character in the range between 0 and 9
_ the literal character _
2nd Alternative: (^[_]+[a-zA-Z0-9_]*)
2nd Capturing group (^[_]+[a-zA-Z0-9_]*)
^ assert position at start of the string
[_]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
_ the literal character _
[a-zA-Z0-9_]* match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
0-9 a single character in the range between 0 and 9
_ the literal character
2)
(.[a-zA-Z][a-zA-Z0-9_]*)|(^[_]*[a-zA-Z0-9_]$)
1st Alternative: (.[a-zA-Z][a-zA-Z0-9_]*)
1st Capturing group (.[a-zA-Z][a-zA-Z0-9_]*)
. matches any character (except newline)
[a-zA-Z] match a single character present in the list below
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
[a-zA-Z0-9_]* match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
0-9 a single character in the range between 0 and 9
_ the literal character _
2nd Alternative: (^[_]*[a-zA-Z0-9_]$)
2nd Capturing group (^[_]*[a-zA-Z0-9_]$)
^ assert position at start of the string
[_]* match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
_ the literal character _
[a-zA-Z0-9_] match a single character present in the list below
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
0-9 a single character in the range between 0 and 9
_ the literal character _
$ assert position at end of the string
3)
(^[a-zA-Z][a-zA-Z0-9_]*)|(^[_]+[0-9]*[a-zA-Z]+[a-zA-Z0-9_]*)
1st Alternative: (^[a-zA-Z][a-zA-Z0-9_]*)
1st Capturing group (^[a-zA-Z][a-zA-Z0-9_]*)
^ assert position at start of the string
[a-zA-Z] match a single character present in the list below
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
[a-zA-Z0-9_]* match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
0-9 a single character in the range between 0 and 9
_ the literal character _
2nd Alternative: (^[_]+[0-9]*[a-zA-Z]+[a-zA-Z0-9_]*)
2nd Capturing group (^[_]+[0-9]*[a-zA-Z]+[a-zA-Z0-9_]*)
^ assert position at start of the string
[_]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
_ the literal character _
[0-9]* match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
0-9 a single character in the range between 0 and 9
[a-zA-Z]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
[a-zA-Z0-9_]* match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
0-9 a single character in the range between 0 and 9
_ the literal character _
4)
(.[a-zA-Z][a-zA-Z0-9_]*)|(^[_]+[a-zA-Z0-9_]*)
1st Alternative: (.[a-zA-Z][a-zA-Z0-9_]*)
1st Capturing group (.[a-zA-Z][a-zA-Z0-9_]*)
. matches any character (except newline)
[a-zA-Z] match a single character present in the list below
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
[a-zA-Z0-9_]* match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
0-9 a single character in the range between 0 and 9
_ the literal character _
2nd Alternative: (^[_]+[a-zA-Z0-9_]*)
2nd Capturing group (^[_]+[a-zA-Z0-9_]*)
^ assert position at start of the string
[_]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
_ the literal character _
[a-zA-Z0-9_]* match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
0-9 a single character in the range between 0 and 9
_ the literal character _
As you can see from above explanations, all 4 regular expressions satisfy part 3) of the question. But only first and third regular expression satisfy part 1) of the question.
Finally, as you can see only regular expression number 3 satisfies all the requirements.
So the answer to this question is:
(^[a-zA-Z][a-zA-Z0-9_]*)|(^[_]+[0-9]*[a-zA-Z]+[a-zA-Z0-9_]*)