Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39919 > unrolled thread
| Started by | "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> |
|---|---|
| First post | 2023-02-10 08:48 -0800 |
| Last post | 2023-02-10 16:37 -0500 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> |
|---|---|
| Date | 2023-02-10 08:48 -0800 |
| Subject | Sonarlint regex |
| Message-ID | <3282c109-3de0-43a9-b2a2-49e135aa52a1n@googlegroups.com> |
My app is passing in a user id. We're hard coding 4 users.
user01
user03
user05
user07
We have validation that the user passed in is one of these.
private void validateUser(String userId) {
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?
[toc] | [next] | [standalone]
| From | Eric Sosman <donalhambra@gmail.com> |
|---|---|
| Date | 2023-02-10 09:50 -0800 |
| Message-ID | <97609095-a286-4935-9c47-946f7edc28d4n@googlegroups.com> |
| In reply to | #39919 |
On Friday, February 10, 2023 at 11:48:12 AM UTC-5, e.d.pro...@gmail.com wrote:
> My app is passing in a user id. We're hard coding 4 users.
> user01
> user03
> user05
> user07
> We have validation that the user passed in is one of these.
> private void validateUser(String userId) {
> 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.)
[toc] | [prev] | [next] | [standalone]
| From | "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> |
|---|---|
| Date | 2023-02-10 10:22 -0800 |
| Message-ID | <bc4def8a-0df3-40b1-9173-ab366548825fn@googlegroups.com> |
| In reply to | #39920 |
> > 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]")
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2023-02-10 16:37 -0500 |
| Message-ID | <ts6de3$15gdh$1@dont-email.me> |
| In reply to | #39921 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web