Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39922
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Sonarlint regex |
| Date | 2023-02-10 16:37 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <ts6de3$15gdh$1@dont-email.me> (permalink) |
| References | <3282c109-3de0-43a9-b2a2-49e135aa52a1n@googlegroups.com> <97609095-a286-4935-9c47-946f7edc28d4n@googlegroups.com> <bc4def8a-0df3-40b1-9173-ab366548825fn@googlegroups.com> |
On 2/10/2023 1:22 PM, e.d.pro...@gmail.com wrote:
>>> if (!userId.matches("(user0)[1|3|5|7]")) { throw new
>>> SecurityException("Invalid user"); } } This seems to work but
>>> Sonarlint flags the regex string with "Remove duplicates in this
>>> character class". Is there a better way to write the same thing?
>> First, try logging in as "user0|". Then ponder what happens. Then
>> lose the "|" characters. (You may also want to lose the "(" and
>> ")", as they seem purposeless.)
> I see. I don't know who came up with that. Apparently the | was
> supposed to mean "or" but that's already the purpose of the [].
> regex101 says the () makes a capturing group which matches the
> characters literally (case sensitive), apparently it does the same
> thing without the () but doesn't call it a capturing group. so just
> .matches("user0[1357]")
"user0[1357]" puts the entire username in group 0
"(user0)[1357]" puts the entire username in group 0
and "user0" in group 1 which does not make sense
"user(0[1357])" puts the entire username in group 0
and "0x" in group 1 which in some contexts could make sense
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
Sonarlint regex "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-02-10 08:48 -0800
Re: Sonarlint regex Eric Sosman <donalhambra@gmail.com> - 2023-02-10 09:50 -0800
Re: Sonarlint regex "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-02-10 10:22 -0800
Re: Sonarlint regex Arne Vajhøj <arne@vajhoej.dk> - 2023-02-10 16:37 -0500
csiph-web