[완료] regex / 대화창에서 URL과 E-mail 링크 추출하는 방법

antz의 이미지

추출해야 하는 것 :
www.domain.com
<a href="http://www.domain.com" rel="nofollow">http://www.domain.com</a>
<a href="http://www.domain.com/~abc?xxx=ddd&xxx2=ddd2" rel="nofollow">http://www.domain.com/~abc?xxx=ddd&xxx2=ddd2</a>
user@domain.com

위에 예로 든 것들이 대화중 섞여 있다면
regex를 어떻게 사용하는게 좋을까요?

프로그램상에서 받아서 URL은
<a href="www.domain.com">www.domain.com</a>
e-mail은
<a href="mailto:user@domain.com>user@domain.com</a>
이렇게 치완해서 보여 주려고 합니다.

다음 코드는 Cocoa(Objective-C)의 코드입니다.

"< code >" 는 indent가 안먹는군요.
"[ code ]" 는 Objective-C의 구문이 "["를 많이 써서 이상하게 되는것 같고요.

텍스트로 같은 코드 입니다.

	int i=0;
	for(i=0;i<[word count]; i++ ) {
		NSString *emailString = @"([a-zA-Z0-9._%+-]+[@]{1}[a-zA-Z0-9._%+-~#%^&*\\/]+)";
		if ( [[word objectAtIndex:i] isMatchedByRegex:emailString] == YES ) {
			NSString *replacementString = @"<a href=\"mailto:$1\">$1</a>";
			[final addObject:[[word objectAtIndex:i] stringByMatching:emailString replace:RKReplaceAll withReferenceString:replacementString]];
		}
		else {
			NSString *urlString = @"(((https?|ftp|gopher|telnet|file|notes|ms-help):(//|\\\\)){0,1}[a-zA-Z0-9._%+-]+[.]{1}[a-zA-Z0-9._%+-~#%^&*\\/]+)";	
			if ( [[word objectAtIndex:i] isMatchedByRegex:urlString] == YES ) {	
				NSString *replacementString = @"<a href=\"$1\" target=\"_blank\">$1</a>";
				[final addObject:[[word objectAtIndex:i] stringByMatching:urlString replace:RKReplaceAll withReferenceString:replacementString]];
#ifdef DEBUG
				NSLog(@"URL Exist!!!");
#endif
			}
			else {
				[final addObject:[word objectAtIndex:i]];
			}
		}
	}

이것 저것 찾아서 해보다가 대충 위처럼 했는데...
대충 되는것 같지만... "..."을 URL로 처리해 버린다는 버그를 받았네요.
<a href="...">...</a>
이렇게 된다는...

File attachments: 
첨부파일 크기
Image icon kldp001.png30.77 KB
김정균의 이미지

http://kldp.net/plugins/scmcvs/cvsweb.php/jsboard-2.1/include/parse.php?rev=1.9;cvsroot=jsboard 에서 auto_link() 함수 참고하세요. GPL 이라면 그냥 들고 사용하시는 것이 더 좋을 겁니다. 제가 생각하기에 제가 만든 것 중에 최고의 작품이라고 생각하는 놈이라서.. 외국에서도 문의가 많이 오는 function 입니다. 속도도 좋으니 그냥 들고 사용하셔도 무방할 겁니다.

송효진의 이미지

antz의 이미지

두분 글 감사합니다.
아직 제대로 보지 못했지만,
우선 고맙다는 말씀을 드립니다. :-)

---

모두 참고해서 어느정도 해결을 했습니다.
고맙습니다. :-)


Blog : http://lum7671.wordpress.com, http://lum7671.egloos.com

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.