문자열 내에서 URL을 찾는 데 사용할 수있는 정규식을 아는 사람이 있습니까? 전체 문자열이 URL인지 확인하기 위해 Google에서 많은 정규 표현식을 찾았지만 전체 문자열에서 URL을 검색 할 수 있어야합니다. 예를 들어, 내가 찾을 수 있도록하고 싶습니다 www.google.com
및 http://yahoo.com
다음 문자열 :
Hello www.google.com World http://yahoo.com
문자열에서 특정 URL을 찾고 있지 않습니다. 문자열의 모든 URL을 찾고 있으므로 정규식이 필요합니다.
답변
이것은 내가 사용하는 것입니다
(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?
나를 위해 일하고 당신에게도 일해야합니다.
답변
정규식이이 용도에 완벽하지 않다고 생각합니다. 여기 에서 꽤 단단한 것을 찾았 습니다
/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/igm
여기에 게시 된 다른 것들과 비교하여 몇 가지 차이점 / 장점 :
- 이메일 주소와 일치 하지 않습니다.
- localhost : 12345와 일치합니다.
moo.com
없이http
또는 같은 것을 감지하지 않습니다www
답변
text = """The link of this question: /programming/6038061/regular-expression-to-find-urls-within-a-string
Also there are some urls: www.google.com, facebook.com, http://test.com/method?param=wasd
The code below catches all urls in text and returns urls in list."""
urls = re.findall('(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+', text)
print(urls)
산출:
[
'/programming/6038061/regular-expression-to-find-urls-within-a-string',
'www.google.com',
'facebook.com',
'http://test.com/method?param=wasd'
]
답변
여기에 제공된 솔루션 중 어느 것도 내가 가진 문제 / 사용 사례를 해결하지 못했습니다.
내가 여기에 제공 한 것은 내가 지금까지 발견 / 만든 것 중 최고입니다. 처리하지 않는 새로운 엣지 케이스를 발견하면 업데이트하겠습니다.
\b
#Word cannot begin with special characters
(?<![@.,%&#-])
#Protocols are optional, but take them with us if they are present
(?<protocol>\w{2,10}:\/\/)?
#Domains have to be of a length of 1 chars or greater
((?:\w|\&\#\d{1,5};)[.-]?)+
#The domain ending has to be between 2 to 15 characters
(\.([a-z]{2,15})
#If no domain ending we want a port, only if a protocol is specified
|(?(protocol)(?:\:\d{1,6})|(?!)))
\b
#Word cannot end with @ (made to catch emails)
(?![@])
#We accept any number of slugs, given we have a char after the slash
(\/)?
#If we have endings like ?=fds include the ending
(?:([\w\d\?\-=#:%@&.;])+(?:\/(?:([\w\d\?\-=#:%@&;.])+))*)?
#The last char cannot be one of these symbols .,?!,- exclude these
(?<![.,?!-])
답변
이 정규식 패턴이 원하는 것을 정확하게 처리한다고 생각합니다.
/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/
다음은 URL을 추출하는 스 니펫 예제입니다.
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// The Text you want to filter for urls
$text = "The text you want /programming/6038061/regular-expression-to-find-urls-within-a-string to filter goes here.";
// Check if there is a url in the text
preg_match_all($reg_exUrl, $text, $url,$matches);
var_dump($matches);
답변
위의 모든 답변은 URL의 유니 코드 문자와 일치하지 않습니다. 예 : http://google.com?query=đức+filan+đã+search
솔루션의 경우 다음이 작동합니다.
(ftp:\/\/|www\.|https?:\/\/){1}[a-zA-Z0-9u00a1-\uffff0-]{2,}\.[a-zA-Z0-9u00a1-\uffff0-]{2,}(\S*)
답변
링크 선택에 엄격해야하는 경우 다음을 수행합니다.
(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
자세한 내용은 다음을 참조하십시오.
