Python regex 일치하는 Url 에

0

질문

나는 목록에서 텍스트 파일의 Url 을 원하지 않는 텍스트가 쓴 regex 는 것입니다 내가 필요로 하는 사항을 만족시키고는 하지만 나는 이 문제를 직면하고 regex 추가 출력을 원하지 않는 샘플["]아래의 예제:

파일 내용의 목록을 Url:

http://www.example.com/52                   (Status: 403) [Size: 919]
http://www.example.com/details              (Status: 403) [Size: 919]
http://www.example.com/h                    (Status: 403) [Size: 919]
http://www.example.com/affiliate            (Status: 403) [Size: 919]
http://www.example.com/56                   (Status: 403) [Size: 919]

regex 나는 사용: "^[://.a-zA-Z0-9-_]*"

출력 아래와 같:

['http://www.example.com/52']
['http://www.example.com/details']
['http://www.example.com/h']
['http://www.example.com/affiliate']
['http://www.example.com/56']

나는 필요한 출력이 다음과 같습니다.

http://www.example.com/52
http://www.example.com/details
http://www.example.com/h
http://www.example.com/affiliate
http://www.example.com/56

코드를 사용에 대한 이 아래 프로그램:

import re

with open("test.txt","r") as test:
    for i in test:
        x = re.findall("^[://.a-zA-Z0-9-_]*",i)
        print(x)
python python-3.x re regex
2021-11-22 04:26:30
1

최고의 응답

0

findall 목록을 생성한 문자열의,당신은 당신을 인쇄 할 수 있습니다 첫 번째 요소에서 결과 print(x[0]) 거나 사용 match 대신 이를 위해 사용하는 경우가 있기 때문에 1url 을 입력합니다.

with open("test.txt","r") as test:
    for i in test:
        x = re.match(r"[://.a-zA-Z0-9-_]*", i)
        print(x.group(0))
2021-11-22 04:43:38

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................