1

I'm new to python programming and doing some challenges to improve my coding; I encounter the following regular expression: (r"^[a-zA-Z][\w_]{2,23}[^_]$").

Doing some research, I understand this:

r" = The expression is a raw string (not sure what this mean entirely)

^[a-zA-Z] = This tells me that the string must start with a letter

[\w_]{2,23} = the body of the string from val[2] to val[23] must be alphanumeric

[^_]$ = the end must be an underscore

If my research is correct, I don't understand why the [\w_] contains an underscore on it. I thought for alphanumerics must be only [\w].

If I'm wrong, help me to clarify it.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    correction ->`[\w_]{2,23}` = there must next be an alphanumeric string with a length anywhere between 2 and 23. – a1cd Dec 01 '21 at 02:59
  • 1
    `[\w_]` = alphanumeric *or* underscore; square brackets define a character set that matches anything listed within it. `[^_]` = anything *except* an underscore; a caret as the first character of a set negates it. – jasonharper Dec 01 '21 at 03:13
  • 1
    @jasonharper `\w` doesn't mean "alphanumeric". Check the [docs](https://docs.python.org/library/re.html#index-32). – wjandrea Dec 01 '21 at 03:14
  • @Evergreen Is right; I needed correction regarding my research. My apologies. – Joaquin Osses Dec 01 '21 at 03:15
  • Are you looking for confirmation about *all* of those points? If so, that's too broad for a Stack Overflow question; instead please ask only one *specific* question at a time, but by all means provide all the details for context. Have you read the docs for the [`re` module](https://docs.python.org/3/library/re.html)? It explains all of the syntax and why to use raw strings. Also if you want a raw string demo, [see the tutorial](https://docs.python.org/3/tutorial/introduction.html#strings). For more tips, see [ask]. [My previous comment to this effect was deleted and I have no idea why.] – wjandrea Dec 01 '21 at 20:56
  • See https://stackoverflow.com/questions/12871066/what-exactly-is-a-raw-string-regex-and-how-can-you-use-it for why a raw string is used. – Barmar Dec 07 '21 at 21:30

1 Answers1

-1

\ is an escape character
\w in regex is considered one character because of the escape character
it means:

any 'word like' (ascii) character [^a-zA-Z0-9_]\

when you do [\w_], this is any word like character OR an underscore. The underscore is not needed as it is included in \w.

a1cd
  • 17,884
  • 4
  • 8
  • 28
  • 1
    An underscore **is** a word character. Read the docs: [`\w`](https://docs.python.org/library/re.html#index-32) – wjandrea Dec 01 '21 at 03:30
  • this is a community wiki if you wanted to change it... – a1cd Dec 01 '21 at 16:31
  • 1
    I'd post my own answer if I wanted to, cause this one doesn't address all of OP's confusions, among other problems. But then, the question itself is too broad. – wjandrea Dec 01 '21 at 21:00