Jump to content

questions about StringRegExpReplace


Recommended Posts

hello sirs

i've some questions about StringRegExpReplace i hope you can help me

 

i tried to make a function that give me the host of the url and other give me the url with out host

for example i've this link

https://www.example.com/vb/result.php

i need the first give me the

example.com

and the other give me 

/vb/result.php

i find that

$s_source = "https://www.google.com/vb/index.php"
Local $s_Host = StringRegExpReplace($s_Source, '.*://(.*?)/.*', '\1')
Local $s_Page = StringRegExpReplace($s_source, '.*://.*?(/.*)', '\1')
msgBox(64, $s_Host, $s_Page)

 

but i found some problems i need your help to correct it

first: when i get the host if the url has www i want to remove it

second: if the url with out host did not have other things 

i need the result to be ""

e.g

https://www.example.com

the first i want it

example.com

and the second i want it to be ""

i hope that you can help me

thanks in advance

Link to comment
Share on other sites

@nacerbaaziz

Use StringRegExp() with the flag $STR_REGEXPARRAYGLOBALMATCH (StringConstants.au3 needed), which will returns an array for each group found based on the pattern set.

This pattern should extract what needed:

'https?:\/\/(?:www\.)?([^\/]+)(.*)'

Post more samples of URLs if you have any :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

For the first : put "www." in a non-capturing group and make it optional
For the second : your regex needs an alternative, "/.*" or end of string

$s_source = "https://www.google.com" ;/vb/index.php"
Local $s_Host = StringRegExpReplace($s_Source, '.*://(?:www.+)?(.*?)(/.*|$)', '\1')
Local $s_Page = StringRegExpReplace($s_source, '.*://.*?(/.*|$)', '\1')
msgBox(64, $s_Host, $s_Page)

Edit
Oops Francesco answered first :sweating:

Edited by mikell
Link to comment
Share on other sites

2 minutes ago, FrancescoDiMuro said:

@nacerbaaziz

Use StringRegExp() eith the flag $STR_REGEXPARRAYGLOBALMATCH (StringConstants.au3 needed), which will returns an array for each group found based on the pattern set.

This pattern should extract what needed:

'https?:\/\/(?:www\.)?([^\/]+)(.*)'

Post more samples of URLs if you have any :)

can you convert the example that i gave you in top please? because i don't know allot on regEx

thx in advance

Link to comment
Share on other sites

Local $sSource = "https://www.google.com/vb/index.php"
Local $aRes = StringRegExp($sSource, '://(?:www\.)?(.*?)(/.+|$)', 3)
MsgBox(64, "URL", "Host = " & $aRes[0] & @LF & "Page = " & $aRes[1])

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

hello
the url that i need to manage it i couldn't know it
because the user who enter the url
for that i have 3 ways
first the url is www.url.com/xxx
the second the url is http(s)://url.com/xxx
the third the url is http(s);://www.url.com/xxx
for that i need to add These possibilities to this example

$s_source = "https://www.google.com/vb/index.php"
or
$s_source = "https://google.com/vb/index.php"
or
$s_source = "www.google.com/vb/index.php"

Local $s_Host = StringRegExpReplace($s_Source, '.*://(?:www.)?(.*?)(/.*|$)', '\1')
Local $s_Page = StringRegExpReplace($s_source, '.*://.*?(/.*|$)', '\1')
msgBox(64, $s_Host, $s_Page)
i hope that can any one help me
I apologize for my delay in understanding and my slow response
thanks in advance

Link to comment
Share on other sites

Local $aURLs = [ _
    "https://www.google.com/vb/index.php", _
    "https://www.google.com/", _
    "https://www.google.com", _
    "https://google.com/vb/index.php", _
    "https://google.com/", _
    "https://google.com", _
    "www.google.com/vb/index.php", _
    "www.google.com/", _
    "www.google.com", _
    "google.com/vb/index.php", _
    "google.com/", _
    "google.com" _
]

Local $aRes
For $Url In $aURLs
    $aRes = StringRegExp($URL, '(?://)?(?:www\.)?(.*?)(/.+|$)', 3)
    ConsoleWrite("URL = " & $URL & @LF & "Host = " & $aRes[0] & @LF & "Page = " & $aRes[1] & @LF & @LF)
Next

 

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

hello again

from all your answers i got this way

it worked with out any problem

this is the way that worked with me

before that please accept my greetings to all of you

and Thank you all for your valuable responses.

here is the code

$s_source = "https://www.mx-blind.com/vb/index.php"
Local $s_Host = StringRegExpReplace($s_Source, '(?:.*://)?(?:www\.)?(.*?)(/.*|$)', '\1')
Local $s_Page = StringRegExpReplace($s_source, '(?:.*://)?(?:www\.)?.*?(/.*|$)', '\1')
msgBox(64, $s_Host, $s_Page)

 

Link to comment
Share on other sites

good morning sirs.
today i've an other question in the same function
i created a html reader
and i need a way to extract the html link
i found the html UDF
but there is a problem
i want to get the url name
e.g i've
$source = '<a href="https://example.com/">the web site</a>
is there any way to get the Link and the name from the tag
here is what i tried to read the name
$s_Name = StringRegExpReplace($source, '(?:<a.*?>)?(?:<.*?>)', '\1')
sometimes it works, and it doesn't work a lot of times.

note
the url can be different from one to an other one
e.g
can be
<a href="https://example.com" id="i" class="newclass"><Div class="nameClass">the web site</div></a>
or
<a class="class" href="https://example.com" id="i">the web site</a>
for that i couldn't use the _StringBetween or a  simple stringReplace
for that i hope any one have any other way.
thanks in advance

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...