Sign in to follow this  
Followers 0
Shanheavel

[Solved] StringRegExp

7 posts in this topic

#1 ·  Posted (edited)

Hello,

I've string:

[D: D, D] A D

where D is digital (0-9), A is some alpha (A-Z) string and D appears as digital or doesn't appear.

So example string can be:

[126: 1, 99] Hello!

or

[1921: 34, 50] Text 501

How can I get these values to array, e.g. $Array = [1921, 34, 50, "Text", 501] ? Edited by Shanheavel

Share this post


Link to post
Share on other sites



Shanheavel,

This looks as if it might do what you want: :D

#include <Array.au3>

$sString = "[126: 1, 99] Hello!"
$aRet = StringRegExp($sString, "w+", 3)
_ArrayDisplay($aRet)

$sString = "[1921: 34, 50] Text 501"
$aRet = StringRegExp($sString, "w+", 3)
_ArrayDisplay($aRet)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Let me suggest something else:

#include <Array.au3>
Local $text[2] = [ _
"[1921: 34, 50] Text 501", _
"[1921: 34, 50] Hello!" _
]
Local $Array[5], $res, $sep = Chr(0x01)
For $i = 0 To UBound($text) - 1
;~  Local $Array[5] = Execute(StringRegExpReplace($text[$i], "[(d+):s*(d+),s*(d+)]s*(S+)s*(d+)?", "[Int($1), Int($2), Int($3), '$4', Int('$5')]"))
$res = StringSplit(StringRegExpReplace($text[$i], "[(d+):s*(d+),s*(d+)]s*(S+)s*(d+)?", "Int($1)"&$sep&"Int($2)"&$sep&"Int($3)"&$sep&"'$4'"&$sep&"Int('$5')"), $sep, 2)
For $j = 0 To UBound($res) - 1
  $Array[$j] = Execute($res[$j])
Next
_ArrayDisplay($Array)
Next

Elements type should be fine and ponctuation characters are allowed in the text part. Could be adjusted if the text part may in fact be a phrase but please specify which characters might be there.

I tried variations using Assign and Execute but it doesn't seem possible to assign an array in one shot by these means.

I won't submit a feature request for allowing that, as it doesn't look like it would be widely used, if ever.


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

jchd,

I did wonder about the missing "!" in my solution, but the OP did say:

"A is some alpha (A-Z) string"

so I decided that it was not required - and it made the SRE much simpler! :D

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

I get it but you know how to treat such rules: wait for exceptional cases found in practice.

But if you look closer, having to later apply Number() to every numeric entry in the array doesn't make the whole process really much simpler.


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

A non RegEx method:

#include <Array.au3>
$string1 = "[126: 1, 99] Hello!"
$string2 = "[1921: 34, 50] Text 501"
$aSplit = StringSplit(StringReplace(StringReplace(StringReplace(StringReplace($string1, "[", ""), "]", ""), ":",  ""), ",", ""), " ", 2)
_ArrayDisplay($aSplit)
$aSplit = StringSplit(StringReplace(StringReplace(StringReplace(StringReplace($string2, "[", ""), "]", ""), ":",  ""), ",", ""), " ", 2)
_ArrayDisplay($aSplit)

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

Thank you all for help!

@UEZ

I used something like it.

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