Sign in to follow this  
Followers 0
jerebenz

StringRegExp Help

10 posts in this topic

Hello All,

I search about an hour about Regular Expression Help (also read the Help 3 times) but I'm still a noob on this.

What I want to do seems very simple, but I don't know how to do it using RegExp :

I have a text that always contains the following string (besides other text) : (Ticket XXX) (where XXX is a number (can be 3 or 4 or n digits long)

I want to only capture the digits from this text.

Share this post


Link to post
Share on other sites



Post the text otherwise help is not possible properly!

BR,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi there,

try this:

$string="Ticket 0158 Anything inbetween here?! Ticket 01242"
$a=StringRegExp($string,"Ticket (\d{3,})?",4)
for $i=0 to UBound($a)-1 Step 1
$match = $a[$i]
MsgBox(0,0,$match[1]) ; match[0] catches the whole Ticket, match[1] only the number
Next

A short explanation for the test-pattern:

The first part "Ticket " means, that the String "Ticket " must be found.

After this string there must be found digits (\d) and at least 3 ({3,}).

The "?" means to find the smallest match (to find the number itself)

Btw: UEZ is right, if you ask such questions you should always post an example text for which your code should work.

Regards

TSGames

Edited by TSGames

Share this post


Link to post
Share on other sites

Hello All,

I search about an hour about Regular Expression Help (also read the Help 3 times) but I'm still a noob on this.

What I want to do seems very simple, but I don't know how to do it using RegExp :

I have a text that always contains the following string (besides other text) : (Ticket XXX) (where XXX is a number (can be 3 or 4 or n digits long)

I want to only capture the digits from this text.

Don't feel lonely. RegExp is like learning to write Chinese until you've been around it for a while. What you want is a non-capturing group to find "Ticket ", and then a group to capture an unknown number of digits:
Global $aInput[5] = ["Ticket 123", _
        "Start of line:Ticket 45678-End of line", _
        "Ticket 90; some more line...", _
        "Begin line, middle line, Ticket 1234567", _
        "Bgining Ticket 123 Middle Ticket 234 End Ticket 345"]
Global $aRET

For $n = 0 To UBound($aInput) - 1
    $aRET = StringRegExp($aInput[$n], "(?:Ticket )(\d+)", 3)
    If IsArray($aRET) And @error = 0 Then
        For $r = 0 To UBound($aRET) - 1
            ConsoleWrite($n & ":" & $r & " = " & $aRET[$r] & @LF)
        Next
    EndIf
Next

:mellow:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I have a text that always contains the following string (besides other text) : (Ticket XXX) (where XXX is a number (can be 3 or 4 or n digits long)

I want to only capture the digits from this text.

Just another solution, it will capture only the 3, 4 or n digits after the word ticket in the whole text:

#include <array.au3> ;Function used only to show the result

$str="Some text here Ticket 345 and more here" & _
"Some text here Ticket 1234 and more here"

$aresult=StringRegExp($str,"(?i)(?<=ticket\s)\d{3,}",3)
_ArrayDisplay($aresult)

Edit: I didn't read that op needed 3,4 or n, I only read 3 or 4 so I've corrected the pattern to match 3 or n digits ;-)

Cheers,

sahsanu

Edited by sahsanu

Share this post


Link to post
Share on other sites

haha, nice one Psalty. Never thought I'd see RegEx's mentioned in a comic strip.:mellow:

Share this post


Link to post
Share on other sites

Well first of all, thanks for your answers (I was leaving office when posted yesterday, and what a surprise to see so much help when I came back !)

@UEZ : Yes, you're right, I should have posted an exemple like : "Thank you, we will process your request. (Ticket 678)"

I know for a fact that there will be only one instance of "(ticket XXX)" in the text, so reading your answers, I tested the following code that is working :

$String="Thank you, we will process your request. (Ticket 674928)"
$Result = StringRegExp($String, "(?:Ticket )(\d+)", 3)
_ArrayDisplay($Result)

There's one more thing though (there's always one with noobs :P).

That's not I don't like to be assisted like this time or that I don't like to post on the forum, but I would like to know how to write regexp.

I once learned php, and I found a very cool tuto that I understood, and I think I can say I would be able to do regexp using php (PCRE).

The thing I don't know, is what type of regexp is using Autoit. After looking in the forum, I didn't find a tuto that applied to autoit (with the same syntax used in autoit).

I know that autoit is using PCRE (well I think so), but when I found links on the forum to help, the syntax was not the same as used in autoit, and then I was not able to understand how to adapt them with autoit :

(http://www.autoitscript.com/autoit3/pcrepattern.html)

(http://regexpal.com/)

:mellow:

Share this post


Link to post
Share on other sites

You need to be aware that there are about as many regexp flavors than hydrogen atoms in the whole universe. No, I'm kidding, but there are enough to get lost.

AutoIt uses PCRE (Perl Compatible Regular Expresssions) which is a software package ... completely independant of Perl (as you wouldn't have guessed).

The complete specifications can be found on the official webpages about the syntax and the patterns. Note that the official webpages may be one or two versions ahead of the version compiled into AutoIt. The PCRE team just released version 8.10 yesterday, while AutoIt uses 8.01 (there was only 8.02 in between).

The entry in AutoIt help file is only a brief introduction and more a reminder than a reference. But at least it discusses the basic pattern specifications and features. That's more than enough to get you started. Look in GeoSoft signature: he has written a nice PCRE testbed which uses the exact PCRE version which AutoIt uses (it is written in AutoIt). Also using the search feature of the forum you'll find mentions of useful websites about regexps as well as nice products like RegExp Coach (free) or RegexBuddy (payware) but AFAIK no one is using the exact same PCRE version as AutoIt, albeit most share 95+% of the features.

Only advanced features are really different in Perl and PCRE (any non jurassic version). The main features and semantics are not going to change as it would break to much code worldwide. PCRE is (in my view) the most advanced, stable and efficient RE engine available around. You'll find PCRE-type regexp in a large number of products so it's worth learning.

What AutoIt doesn't implement right now is the [Perl] callback capability (oh yes there's another thing: AutoIt doesn't offer the DFA engine).. Also every pattern is [re]compiled before use: there's currently no provision for storing compiled patterns for subsequent use. That may change someday.


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 Jchd,

Thanks for your detailed answer. I feel I now have all the keys to learn how to use Regexp in Autoit !

Thanks all :mellow:

Bye

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
Sign in to follow this  
Followers 0