Question

In: Computer Science

Write the RE for identifiers that can consist of any sequence of letters (l) or digit...

Write the RE for identifiers that can consist of any sequence of letters (l) or digit (d) or "_"
but the first char must be a letter and the last char cannot be a "_" (10)

Solutions

Expert Solution

RE is

/^(([A-Za-z])|([A-Za-z][A-Za-z0-9_]*[A-Za-z0-9]))$/

What each word signify:

^ : start of string
[ : beginning of character group
a-z : any lowercase letter
A-Z : any uppercase letter
0-9 : any digit
_ : underscore
] : end of character group
* : zero or more of the given characters
| : or operation
( : used for grouping regex
):used for grouping regex
$ : end of string

It can be divided into two parts:

  • First one [A-Za-z] which means the first character can be just from Capital A-Z or Small a-z and also it is the last character so that means a string with only one value so it will be just a letter as first character needs to be a letter and last one cannot be underscore
  • There is | - OR in between the first and second part
  • Second one can be broken into three parts:
    • First one [A-Za-z] which means the first character can be just from Capital A-Z or Small a-z
    • Second part [A-Za-z0-9_]* which all the characters in between can be from Capital A-Z or Small a-z or digit 0-9 or an underscore and this can occur zero or multiple times so * is placed outside the bracket
    • Then comes the third and the last part [A-Za-z0-9] in which it can be from Capital A-Z or Small a-z or digit 0-9 but not an underscore and will be the last character it can be only one letter or a digit

The first part is their to help regex use for a single letter input every other case will handled by the second part


Related Solutions

Write the RE for identifiers that can consist of any sequence of letters (l) or digit...
Write the RE for identifiers that can consist of any sequence of letters (l) or digit (d) or "_" but the first char must be a letter and the last char cannot be a "_"
Describe the rules governing identifiers. Can identifiers be re-used in the same Java class file? Provide...
Describe the rules governing identifiers. Can identifiers be re-used in the same Java class file? Provide three examples of different kinds of invalid identifiers. Java
1An identification code is to consist of 2 letters followed by 5 digit. Determine the fallowing?...
1An identification code is to consist of 2 letters followed by 5 digit. Determine the fallowing? 1a) how many different codes are posible if repetition is permitted? 1b) how many different codes are possible if repetition is not permitted, but the first 2 letters must ve the same letter? 1c) how many different codes are possible if the first letter must be, P, Q, R, S, T, and repetition for everything else is not permitted?
Write a regular expression that can represent any digit, without more than one digit or non...
Write a regular expression that can represent any digit, without more than one digit or non digit characters.
write a program that uses exactly four for loops to print the sequence of letters below...
write a program that uses exactly four for loops to print the sequence of letters below A A A A A A A A A A B B B B B B B C C C C C C E E E E E
How many: New York license plates can be made that consist of 3 letters followed by...
How many: New York license plates can be made that consist of 3 letters followed by 4 numbers given the restriction that if the first letter is an ‘N’, then the second letter must be a ‘Y’? New York license plates can be made that consist of 3 letters followed by 4 numbers given the restrictions that (1) if the first letter is an ‘N’, then the second letter must be a ‘Y’ and (2) if the second letter is...
Write a function flipSwitches that accepts one argument, a sequence of upper or lower case letters...
Write a function flipSwitches that accepts one argument, a sequence of upper or lower case letters (the sequence can either be a str or a list, if you write your code correctly, it shouldn’t matter). This is a sequence of switches, uppercase means to turn a switch on, and lowercase to turn a switch off.   For example, ‘A’ means to turn the switch ‘A’ on, and ‘a’ means to turn the switch ‘A’ off.   (Turning an on switch on again,...
The user enters some text into a textbox. It can contain any characters (letters, digits, spaces,...
The user enters some text into a textbox. It can contain any characters (letters, digits, spaces, punctuation marks, and there is no length limit). When a button Count is hit , display the number of upper-case letters in the inputted phrase. This must be done in visual studios using C#.
Exercise 1: You are required to re-write the following code to catch the exception that can...
Exercise 1: You are required to re-write the following code to catch the exception that can occur. import java.util.Scanner; public class ExceptionDemo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an integer: "); int number = scanner.nextInt(); // Display the result System.out.println( "The number entered is " + number);           } }
Q4. The sentences below may or may not contain errors, Re-write them, correcting any errors you...
Q4. The sentences below may or may not contain errors, Re-write them, correcting any errors you find to make them acceptable without changing their meanings. i. The invited guest for the programme is still not in; it’s already 2 o’clock pm. ii. All what they needed to do was to apologize to the victims. iii. Many people are happy about what president said in his speech that, “…, but what we don’t know is to bring people back”. iv. We...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT