Jump to content
Tuts 4 You

[ask, Delphi] how to extract URLs from TMemo


X-88

Recommended Posts

how to get URLs in memo, delimited text or split?

e.g:

in memo.text :

begin

memo1.lines.text := '

rtadgjkjuouioop hxxp://exmpl.com yyhfhjjj

ijoo hxxp://wxw.exmpl.org iolvb

wxw.exmpl.comiiij

fiuddhity

ftp://exmpl.com

uiufsftgco45788

hxxps://exmpl.com

ggcghj

hxxps://wxw.exmpl.net giiu';

end;

result in memo 2

I mean like this code:

function spliter(const s : string; ts, rs : tstrings) : string;

var

i : integer;

begin

for i := 0 to rs.count - 1 do

begin

if (pos('wxw', ts.strings) > 0) or

(pos('fxp://', ts.strings) > 0) or

(pos('hxxp://', ts.strings) > 0) or

(pos('hxxps://', ts.strings) > 0) or

(pos('hxxp://wxw', ts.strings) > 0) or

(pos('hxxps://wxw', ts.strings) > 0) then

begin

result := '';

blah blah blah

etc

end;

rs.text := result;

end;

on button click

spliter(edit1.text, memo1.lines, memo2.lines);

Link to comment

Use a regular expression instead of multiple compares


 


something like this works great for a URL... (From RegexBuddys library)



\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[A-Z0-9+&@#/%=~_|]

But beware, a regex or any sort of compare will never be 100% accurate for a URL because a URL can contain most characters, especially if you are trying to filter spam and the poster is trying to bypass the filter...


Link to comment

I'm not sure what you asking but if your asking how to match / extract the link in your example ' to' then use a regex again, something like this...



a.?href="'(.+)'"
Link to comment
  • 1 month later...

if you want to use pos then use it with the '://' string - 4 chars for the beginning and the last '.' + 3 chars for the end per line, this should yield full url. regular expressions would be the best way for this and the more correct way to do it.


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...