JAVA Programing. Write regular expressions after the to validate the following s
ID: 3843999 • Letter: J
Question
JAVA Programing.
Write regular expressions after the to validate the following strings:
a) An International Standard Book Number (ISBN). This is a unique 13-digit number assigned to each edition of a book. An ISBN contains hyphens. Here is a sample: 978-3-16-148410-0. Assume that each digit is in the range 0-9.
b) A chemical symbol of an element from the Periodic Table.
c) A measurement including a whole number and units separated by one space. A sample measurement is 34 oz. Assume that units is lower case letters only.
d) A two-character abbreviation for any of the eight US states or the Canadian province adjacent to the Great Lakes. Assume the abbreviation is upper case characters only.
Explanation / Answer
The regular expression for chemical element from periodic table and ISBN-13 is quite long as there are many variation to the way in which they can be represented so this is the sortest
A-) String regex = "^(?:ISBN(?:-13)?:? )?(?=[0-9]{13}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)
97[89][- ]?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9]$";
B-) String regex = "
C-) String regex = "([0-9]{2} [a-z]{2})";
D-) String regex = "([A-Z]{2})";
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.