0
đ java
5. Write a regular expression that determines whether the given string is a hexadecimal color identifier in HTML. Where #FFFFFF is for white, #000000 for black, #FF0000 for red, etc. â an example of correct expressions: #FFFFFF, #FF3421, #00ff00. â an example of incorrect expressions: 232323, f#fddee, #fd2.
1 Answer
+ 4
Try to describe the expression in words before jumping to regex.
The string always starts with '#'.
There are 6 possible values after the '#'.
Each of those values are from 0 to 9 or a to f.
Those values can also be capitalized A to F.
Here is my regex:
^#[0-9a-fA-F]{6}$
^ start of the string (might be optional)
[0-9] being from 0 to 9
[a-fA-F] being from a to f and A to F
{6} repeat 6 times
$ end of the string (might be optional)
Last note, regex is not specific to Java it is a powerful tool that is being used across multiple languages, almost everywhere!
I recommend you to look into https://regex101.com it's a great site to help with regex.
I hope this helps :)