Jump to content

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

Share this post


Link to post
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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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)

Share this post


Link to post
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

Share this post


Link to post
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)

Share this post


Link to post
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)

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

good morning

sir

@mikell

the url is worked

what about how to get the url name

i tried to use the code that i gave it above but some times it not working

i hope if any one can help me to do that.

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By HariKara
      I have a script that seems to launch perfectly fine with IECreate, however, I want to launch the Browser and a specific URL with the RunAs command. I fairly new to AutoIT and wanted to know if someone can point me in the right direction.
      Local $surl = "http://somewebsite.com/DODA/admin/job.aspx"
      Local $oIE = _IECreate($sURL)
      The above launches the website correctly with the correct URL, however, I have tried the below and this fails to load and browser?
      #include <AutoItConstants.au3>
      ;========================
      $oSleep = "200"
      Local $surl = "http://somewebsite.com/DODA/admin/job.aspx"
      Local $oIE = RunAs($args("username"), $args("domain"), $args("logonpassword"), "", "C:\Program Files (x86)\Internet Explorer\iexplore.exe http://somewebsite.com/DODA/admin/job.aspx")
      _IELoadWait($oIE)
       
      I get the following error back?
      --> IE.au3 T3.0-2 Error from function _IELoadWait, $_IESTATUS_InvalidDataType
      --> IE.au3 T3.0-2 Error from function _IEGetObjById, $_IESTATUS_InvalidDataType
      --> IE.au3 T3.0-2 Error from function _IEGetObjById, $_IESTATUS_InvalidDataType
    • By genius257
      Inspired by PHP's preg_split.
      Split string by a regular expression.
      Also supports the same flags as the PHP equivalent.
      v1.0.1
       
      Example:
      #include "StringRegExpSplit.au3" StringRegExpSplit('splitCamelCaseWords', '(?<=\w)(?=[A-Z])') ; ['split', 'Camel', 'Case', 'Words']  
    • By jmp
      i am trying to get number from string using this code :
      #include <IE.au3> $oIE = _IEAttach ("Edu.corner") Local $aName = "Student name & Code:", $iaName = "0" Local $oTds = _IETagNameGetCollection($oIE, "td") For $oTd In $oTds If $oTd.InnerText = $aName Then $iaName = $oTd.NextElementSibling.InnerText $iGet = StringRegExpReplace($iaName, "\D", "") EndIf Next MsgBox(0, "", $iGet) it was get number like 52503058
      But, I want to get only student code 5250. (Different student have different code, sometime its 3 digits, Sometime 4)

       
    • By jmp
      I am adding labour charge to total paid amount using : 
      #include <IE.au3> #include <Array.au3> $oIE = _IEAttach ("Shop") $oTable = _IETableGetCollection ($oIE, 1) $aTableData3 = _IETableWriteToArray ($oTable) Local $sitem1 = $aTableData3[5][1] Local $sitem2 = $aTableData3[5][2] Local $lcharge = "10" ;add manualy using inputbox, becuase not generating online Local $atotPric = "Payable Total Price " Local $oTds = _IETagNameGetCollection($oIE, "td") For $oTd In $oTds If $oTd.Innertext = $atotPric Then $iatotPric = $oTd.NextElementSibling.innertext MsgBox (0, "2", $iatotPric) EndIf Next $irCtotal = StringFormat("%.2f", $sitem1 + $sitem2 + $lcharge) $crTotp = StringReplace(_IEBodyReadHTML($oIE), $iatotPric, $irCtotal) _IEBodyWriteHTML ($oIE, $crTotp) But, It was also changing Total price, I want to change only Payable Total Price.

×
×
  • Create New...