Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #39922

Re: Sonarlint regex

Path csiph.com!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Sonarlint regex
Date Fri, 10 Feb 2023 16:37:02 -0500
Organization A noiseless patient Spider
Lines 28
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>
MIME-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Fri, 10 Feb 2023 21:37:07 -0000 (UTC)
Injection-Info reader01.eternal-september.org; posting-host="950f8bf09d87aeac7f805fb3d29fc2e0"; logging-data="1229233"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/+xiQFBwN8XSezsGNQ66zuCMfMwQOuByw="
User-Agent Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.7.2
Cancel-Lock sha1:R0vA3II3hRPehIyN/lwcVLc/SSg=
In-Reply-To <bc4def8a-0df3-40b1-9173-ab366548825fn@googlegroups.com>
Content-Language en-US
Xref csiph.com comp.lang.java.programmer:39922

Show key headers only | View raw


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


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