Reputation: 75
I need help creating a regEx for checking that inputs are
For the first, 17 characters part, I've solved it (according to the HW requirements) by using if(str.length() == 17)
, and I've sort of understand how to do the excluding characters part. Here's what I have so far
String regEx = "\\S^[I,O,Q][A-Z]"
but this does not work. I'm kind of stumped on how to get it working. I know that \\S
means that it cannot include any white spaces. The ^
means that it should exclude I,O,Q
(or so I think it should, not sure), and then [A-Z]
means it should all be in capital letters.
Can anyone help me figure out how to properly make the regEx for this?
Side notes, I've looked at other regEx questions relating to removing special characters, but it only excludes a few $<>@&
, how do I get it to exclude all of them?
Thanks
Upvotes: 1
Views: 89
Reputation: 1689
You can use this regex:
[A-HJ-NPR-Z]{17}
I have created a list of random words with length 17, so you can see this regex in action:
https://regex101.com/r/w7zMEY/1
try yourself adding more random words
Side notes, I've looked at other regEx questions relating to removing special characters, but it only excludes a few $<>@&, how do I get it to exclude all of them?
with this regex
[A-HJ-NPR-Z]{17}
you are matching only capital letters, except for I,O,Q... any other char it's excluded
Note that you may need to surround the regex with something else... that depends on the context in which your are looking for those words; for example:
If you want to match that pattern, but a sole word per line, you can use:
^[A-HJ-NPR-Z]{17}$
see here: https://regex101.com/r/w7zMEY/2
if you want to match that pattern, in a 'random' place on a line, but only if it is not surrounded by other capital letters, you can use:
(?<=[^A-Z])[A-HJ-NPR-Z]{17}(?=[^A-Z])
see here: https://regex101.com/r/w7zMEY/3
Upvotes: 1