Jump to content

Regex


gimx
 Share

Recommended Posts

Hello,

I have variables in my script. The fist variable is a digit $digit which is given by user via an input. I have an array $data. Datas in array are always the same pattern : A[number]B[number] [2 numbers] (for example "A2B3 12", letters never change only numbers).

Now i want to compare $digit with $data.

If the first number in $data (A2B3 12) isn't the same than $digit then i don't want this data.

If the first number in $data (A2B3 12) is the same than $digit, i want a new data in array for example, but i want only the the number after B and the lasts 2 numbers (A2B3 12 -> 312).

I suppose i must use regex but i don't understand how i can do this script.

If someone can give me an example please.

Thx :)

Edited by gimx
Link to comment
Share on other sites

I'm afraid I don't understand exactly what you are looking to do

Here is the best I can give you...

#include <Array.au3>
$String = "A2B3 12"
$Result = StringRegExp($String, "\d{1,2}",3)
MsgBox(0,"",$Result)
_ArrayDisplay($Result)

This returns each number in the array:

$Result[0] = the number after "A"

$Result[1] = the number after "B"

$Result[2] = the 2-digit number on the end

Link to comment
Share on other sites

Thx for your reply.

Sorry you can't understand because i have a poor english and i can't explain what i want :P

The user have an input box where he write a number, for example 2, click OK.

In my script, i get this value in a variable $digit.

I have an array of datas like that :

$data[0] = A2B3 12

$data[1] = A1B5 09

$data[2] = A3B9 15

$data[3] = A2B7 03

...

I want to compare array with $digit.

If $digit = the first number of array then insert data in new array.

In my example with $digit=2, the new array is :

$newdata[0] = 312 , because the first number is same than $digit (2) : $data[0] = A2B3 12

$newdata[1] = 703 , for $data[3] = A2B7 03

I hope it's clear... :)

Edited by gimx
Link to comment
Share on other sites

Thx for your reply.

Sorry you can't understand because i have a poor english and i can't explain what i want :P

The user have an input box where he write a number, for example 2, click OK.

In my script, i get this value in a variable $digit.

I have an array of datas like that :

$data[0] = A2B3 12

$data[1] = A1B5 09

$data[2] = A3B9 15

$data[3] = A2B7 03

...

I want to compare array with $digit.

If $digit = the first number of array then insert data in new array.

In my example with $digit=2, the new array is :

$newdata[0] = 312 , because the first number is same than $digit (2) : $data[0] = A2B3 12

$newdata[1] = 703 , for $data[3] = A2B7 03

I hope it's clear... :)

$var = "A2" ;<==or whatever you are looking for in this array
For $i = 1 to $data[0]
  If StringInStr($data[$i], $var)  Then
     ;do whatever you want with it here
     MsgBox(0, "", $data[$i])
     ExitLoop ;<==If you want to stop
  EndIf
Next
Edited by Oldschool
Link to comment
Share on other sites

Sorry but i have another question with regex because i don't understand all switch or option.

How can i test a string ?

I get html code and i want some words between tag.

For example, i want to see if <b>city : [string]</b> exist in the page and if it exist, i want to get the string after the word "city :".

<b>city : paris</b>, i want get "paris" in a variable.

Thx :)

Link to comment
Share on other sites

Hi,

have a look at _StringBetween.

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Thx for the function, i don't know it.

I try, but this function works if you know the string you search.

But in my example with Paris, i don't know which town is after "city :". Paris is an example.

But i think it's a good way and i try with this function :)

PS : if someone know a good regex tutorial... :P

Edited by gimx
Link to comment
Share on other sites

Hi,

you just need the start point and end point. That's all.

Learning regex is easy when you got exampes what you want to achieve.

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Sorry but my code don't work with _stringbetween.

I have parse html in a variable.

$var1 = "<strong><a href="http://www.blablabla.com" onclick="popUp(...);return false;"> BlaBlaBla</a></strong>"

I want the link in bold but the getlink function don't work, so i try _stringbetween but don't work.

_StringBetween($var1, '<a href="', '" onclick=')

What's wrong ?

Thx :)

Link to comment
Share on other sites

Hi,

try this pattern with StringRegExp (?<=<a href=").*?(?=")

So log,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Thx but don't work.

I think i understand why the function getlink don't work, because the link is "incomplete", it's like that :

<a href="/poopoopidoo/blablabla.com" onclick="popUp(...)

And i think the _stringbetween don't work with several strings in start and end arguments.

So... I'm with the same problem... :)

Link to comment
Share on other sites

Hi,

post a fre examples and I'll give you a working pattern.

Link --> what you want

Link --> what you want

Link --> what you want

So long,

Mega

Global $str = '<strong><a href="http://www.blablabla.com" onclick="popUp(...);return false;"> BlaBlaBla</a></strong>'
Global $str1 = '<a href="/poopoopidoo/blablabla.com" onclick="popUp(...)'
$link = StringRegExp($str, '(?<=<a href=").*?(?=")', 1)
MsgBox(64, 'Link', $link[0])
$link = StringRegExp($str1, '(?<=<a href=").*?(?=")', 1)
MsgBox(64, 'Link', $link[0])
Edited by Xenobiologist

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Thx :)

I get html code in a TD.

Now, i want the link in bold : /fiche/1205/tartenpion

<strong><a href="/fiche/1205/tartenpion" onclick="popUp(this.href, 'data', 'fiche', 300,300);return false;"> tartenpion</a></strong>

After i can complete to add the domain like that :

$url = "www.blablabla.com" & $link

But i must get the link before :P

Edited by gimx
Link to comment
Share on other sites

Hi,

sorry I do not understand? Did my code not work for you?

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

No it don't work.

$html = $oTR.firstChild.nextSibling.innerHTML
                            $link = StringRegExp($html, '(?<=<a href=").*?(?=")', 1)
                            $url = "www.blablabla.com/" & $link[0]
MsgBox(64, "test", $url)

==> Subscript used with non-Array variable.: 
$url = "www.blablabla.com/" & $link[0] 
$url = "www.blablabla.com/" & $link^ ERROR
Edited by gimx
Link to comment
Share on other sites

Hi,

you see that the pattern works in my test script. When $html cotains no link then $link isn't an array.

You have to check that. If isarray($link) Then ....

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Sorry but i have another question.

I have this string : 2c3f5e(2568)8f4e1a

I want only strings like that number+letter. In the example, the result must be 2c 3f 5e 8f 4e 1a.

Wich pattern can i use plz ?

I use : (([0-9][a-z])*) but it don't work.

Thx :D

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...