I can extract characters well on the web, but I can't use regular expressions with python.

Asked 1 years ago, Updated 1 years ago, 67 views

I'd like to extract the string from the sentence using the regular expression below.

[\w-]+(\.[\w-]+)*

When I checked on the web to see if this regular expression was correct, I was able to extract the characters as shown in the figure below.

  • Example
    Enter a description of the image here

I tried to do the same thing with Python, but it didn't work.

test_data='String you want to search for'
result=re.findall(r'/*[\w-]+(\.[\w-])+) *', test_data)

Perhaps there is something wrong with the Python code.
Please tell me where the problem is.
I look forward to your kind cooperation.

python regular-expression

2022-09-30 15:38

2 Answers

Well, what I want to extract is

  • One or more characters
  • First letter is alphanumeric or -
  • The second and subsequent characters include alphanumeric characters and -, plus .

Isn't it?
In that case, r'[\w-][\w\.-]*' is fine.


2022-09-30 15:38

Use non-capturing match group to get results similar to JavaScript's regular expression engine.

Regular Expression Syntax

(?:...)

An-capturing version of regular parents.Matches whatver regular expression is inside the parents, but the substring matched by the group cannot be retried after performing a match or referred later in the pattern.

import re

test_data='"
['Conflicting with version dependencies when running pip install',
"Having issues with version dependencies when running `pip install` on docker.
However, when installing on my mac without docker and just virtualenv, works perfect fine.
**Mac OS**-macOS Mojave v10.14.**Python Version**-v3.7.3**Docker Composer Version**-
version 1.27.4, build 40524192 Here's the first error I got when running the docker-compose
up I tried to loosen the `six` package from `six==1.10.0` to `six>=1.2,<=1.15.0`Androws
different error for requests package this time. Here's the error
'''

result=re.findall(r'[\w-]+(?:\.[\w-]+) *', test_data)
print(result)

## Results (line breaks are included accordingly)
['Conflicting', 'with', 'version', 'dependencies', 'when', 'running', 'pipe',
 'install', 'Hi', 'everyone', 'Having', 'issues', 'with', 'version', 'dependencies',
 'when', 'running', 'pipe', 'install', 'on', 'docker', 'however', 'when', 'installing',
 'on', 'my', 'mac', 'without', 'docker', 'and', 'just', 'virtualenv', 'works',
 'perfectly', 'fine', 'Mac', 'OS', '-', 'macOS', 'Mojave', 'v10.14', 'Python',
 'Version', '-', 'v3.7.3', 'Docker', 'Compose', 'Version', '-', 'version', '1.27.4',
 'build', '40524192', 'Here', 's', 'the', 'first', 'error', 'I', 'got', 'when',
 'running', 'the', 'docker-compose', 'up', 'I', 'tried', 'to', 'loosen', 'the',
 'six', 'package', 'from', 'six', '1.10.0', 'to', 'six', '1.2', '1.15.0', 'And',
 'throws', 'a', 'different', 'error', 'for', 'requests', 'package', 'this', 'time',
 "Here", "s", "the", "error"]

There is another way to use re.finder.

result=[m.group(0)for min re.finder(r'[\w-]+(\.[\w-]]+)*', test_data)]


2022-09-30 15:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.