I have a question about regular expression pattern extraction

Asked 2 years ago, Updated 2 years ago, 16 views

I wanted to extract lovely and real ending with ly from sentence.

Doesn't r'\D+ly' become 'repeat non-numeric (\D) several times (+), and find a pattern that ends with ly'?

But I wonder why the result was different from r'\w+ly'.

r'\w+ly' thinks that even if the letter or number (\w) is repeated many times (+), find a pattern that ends with ly', so I think the right result came out.

Shouldn't r'\D+ly' also have ['lovely', 'really']? I'm curious

Thank you for reading.

python

2022-09-20 19:01

1 Answers

Because \D contains spaces and \w does not contain spaces. https://regex101.com/r/W91E8U/1


2022-09-20 19:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.