0

I'm discovering the regex between /(\\r|\\n|\*|\:)/ and /^\[[^*\\:\[\]'\r\n]{0,200}\]/

The first regex is \r or \n or * or :, and the second regex is any character except for * or \ or : or [] or ' or \r or \n in square bracket til 200 times.

What I'd need to make was allowing any character except for * or \ or : or [] or ' or \r or \n in square bracket til 200 times, so I made the second regex.

Then I found someone wrote the first regex, and was curious whether I need to implement the second regex similar to the first one using () |.

I know the fist one is for grouping and capturing, but needs more steps to check. And the second one is just for optimized one with limit with group and capture.

Is there anyone who can add more knowledge to what I know? Thanks.

eileen503
  • 31
  • 3
  • Does this answer your question? https://stackoverflow.com/questions/9801630/what-is-the-difference-between-square-brackets-and-parentheses-in-a-regex – Dennis Kats Mar 30 '23 at 00:08
  • Does this answer your question? [What is the difference between square brackets and parentheses in a regex?](https://stackoverflow.com/questions/9801630/what-is-the-difference-between-square-brackets-and-parentheses-in-a-regex) – Dennis Kats Mar 30 '23 at 00:09
  • The first method doesn't easily extend to "all characters except ...". – Barmar Mar 30 '23 at 00:09
  • But square brackets can only match single characters, while alternatives in parentheses can match longer patterns. – Barmar Mar 30 '23 at 00:09
  • So use whichever one best matches your needs. Character sets in square brackets are easier to read than a long list of alternatives in parentheses, so it should be preferred when appropriate. – Barmar Mar 30 '23 at 00:11

1 Answers1

0

The short answer is... no. The [...] notation creates a character class.

Your (\\r|\\n|\*|\:) is exactly equivalent to [\r\n*:][1] (and will, if the regex engine is any good, result in the generation of exactly the same state table.

One of those two variants, however, is easier to understand.

[1] Except for the capturing group. If you want that, you'll need ([\r\n*:]).

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135