Jump to content

StringRegEx Extract five-digit number


Go to solution Solved by Melba23,

Recommended Posts

Hello again,

i´m a little bit in trouble trying to extract the five-digit number from a string

i have an array like this:

 

array                                                     arraynew

row1   112 56985 1 589 6                 56985

row2   15896 25 25 3636                  15896

row3   6 6 8956 56895 4                   56895

....

Does someone have an good idea?

 

Thank you for your help.

 

 

Link to post
Share on other sites
  • Moderators
  • Solution

Felicitas:

Try this:

#include <Array.au3>

Global $aArray[] = ["112 56985 1 589 6", "15896 25 25 3636", "6 6 8956 56895 4"]

Global $aNewArray[UBound($aArray)]

For $i = 0 To UBound($aArray) - 1
    ; Get array of all 5 digit numbers in element
    $aRet = StringRegExp($aArray[$i], "\d{5}", 3)
    ; Add returned string to the new array
    $aNewArray[$i] = $aRet[0]
Next

_ArrayDisplay($aNewArray, "", Default, 8)

I leave error-checking to you - and no doubt a real guru will be along soon to do the whole thing in one line!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to post
Share on other sites
5 hours ago, Melba23 said:

do the whole thing in one line

No way to do a one-liner using StringRegExp directly on an array  :)
The only one I can think of is this

#include <Array.au3>

Global $aArray[] = ["112 56985 1 589 6", "15896 25 25 3636", "6 6 8956 56895 4"]

Global $aNewArray = StringRegExp(_ArrayToString($aArray), "\d{5}", 3)
_ArrayDisplay($aNewArray, "", Default, 8)

 

Link to post
Share on other sites

thank you so much, it works perfectly...

is there an option to get an array with the empty rows?

for example:

row1   112 56985 1 589 6                 56985

row2   15 4 123 23 623 8                  "Null"                           

row3   6 6 8956 56895 41                 56895

row4                                                    "Null"

row5 15557 15 1 1 1556                  15557

 

thank you for your help :)

 

Link to post
Share on other sites
  • Moderators

Felicitas,

Quote

is there an option to get an array with the empty rows?

Of course:

#include <Array.au3>

Global $aArray[] = ["12 56985 1 589 6", "15 4 123 23 623 8", "6 6 8956 56895 41", "", "15557 15 1 1 1556"]

Global $aNewArray[UBound($aArray)]

For $i = 0 To UBound($aArray) - 1
    ; Get array of all 5 digit numbers in element
    $aRet = StringRegExp($aArray[$i], "\d{5}", 3)
    If Not @error Then
        ; Add returned string to the new array
        $aNewArray[$i] = $aRet[0]
    Else
        ; Or place "Null"
        $aNewArray[$i] = "Null"
    EndIf
Next

_ArrayDisplay($aNewArray, "", Default, 8)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to post
Share on other sites

for funzies... Idk why the non capturing group wasn't working. then i looked at the documentation to see it was actually in there then came back and it was working.... weird like im being spied on....

#include <Array.au3>

Global $aArray[] = ["112 56985 1 589 6", "15896 25 25 3636","", "6 6 8956 56895 4"]

for $i in $aArray
$ticket = StringRegExp($i, "^(?:.*\W*)(\d{5})(?:.*)$|(^$)", 1)
if IsArray($ticket) Then _ArrayDisplay($ticket)
Next

 

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.

×
×
  • Create New...