Jump to content
Alexxander

Regex expression

Recommended Posts

Alexxander

i have lots of lines like this:

Geek - Smarter Shopping 1.1.5

Soccer Stars 1.4.3

Rock Hero 1.1.3

Pinterest 4.6.1

Winter Craft 3: Mine Build 1.1.5

Flick Shoot 2 1.25

 

 

 

i want to split the name from the version

$arr1 = Pinterest @arr2 = 4.6.1

 

what would the most precise way to do this ?

maybe split it when autoit find a word then a number (decimal or integer) 

 

what would the regex split command be for a goal like this ? 

 

Edited by Alexxander

Share this post


Link to post
Share on other sites
Melba23

Alexxander,

This seems to work quite well:

#include <Array.au3>

$sText = "Geek - Smarter Shopping 1.1.5" & @CRLF & _
    "Soccer Stars 1.4.3" & @CRLF & _
    "Rock Hero 1.1.3" & @CRLF & _
    "Pinterest 4.6.1" & @CRLF & _
    "Winter Craft 3: Mine Build 1.1.5" & @CRLF & _
    "Flick Shoot 2 1.25"

$aRet = StringRegExp($sText, "(?mU)^(.*)([\d.]*)$", 3)

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

SRE decode:

(?mU)       - Treat all lines as separate strings - look for shortest match
^       - Start of the line
(.*)        - Capture all characters up until the next group
([\d.]*)    - Capture any group made up of digita and dots until...
$       - the end of the line

No doubt a real guru will be along shortly with a better answer.

M23

  • Like 1

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

 

Share this post


Link to post
Share on other sites
Alexxander

Melba23

Thanks bro, was missing you ,i had not seen you around for a while ...


Share this post


Link to post
Share on other sites
Alexxander

Alexxander,

This seems to work quite well:

#include <Array.au3>

$sText = "Geek - Smarter Shopping 1.1.5" & @CRLF & _
    "Soccer Stars 1.4.3" & @CRLF & _
    "Rock Hero 1.1.3" & @CRLF & _
    "Pinterest 4.6.1" & @CRLF & _
    "Winter Craft 3: Mine Build 1.1.5" & @CRLF & _
    "Flick Shoot 2 1.25"

$aRet = StringRegExp($sText, "(?mU)^(.*)([\d.]*)$", 3)

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

SRE decode:

(?mU)       - Treat all lines as separate strings - look for shortest match
^       - Start of the line
(.*)        - Capture all characters up until the next group
([\d.]*)    - Capture any group made up of digita and dots until...
$       - the end of the line

No doubt a real guru will be along shortly with a better answer.

M23

this did't worked for me when i faced this :

 

Spider-Man Unlimited 1.4.1a

 

any ideas ?


Share this post


Link to post
Share on other sites
iamtheky

If there are never going to be spaces in the version then splits should do:

#include <Array.au3>

$sText = "Geek - Smarter Shopping 1.1.5" & @CRLF & _
    "Soccer Stars 1.4.3" & @CRLF & _
    "Rock Hero 1.1.3" & @CRLF & _
    "Pinterest 4.6.1" & @CRLF & _
    "Winter Craft 3: Mine Build 1.1.5" & @CRLF & _
    "Flick Shoot 2 1.25" & @CRLF & _
    "Spider-Man Unlimited 1.4.1a"

$aText = stringsplit($sText , @LF , 2)

for $i = ubound($aText) - 1 to 0 step - 1
$aLine = stringsplit($aText[$i], " " , 2)
$aText[$i] = StringReplace($aText[$i] , $aLine[ubound($aLine) - 1] , "")
$Test = $i = ubound($aText) - 1 ? _ArrayAdd($aText , $aLine[ubound($aLine) - 1]) : _ArrayInsert($aText , $i + 1 , $aLine[ubound($aLine) - 1])
next


_ArrayDisplay($aText)

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
Melba23

Alexxander,

I hate it when people suddenly produce edge cases - but this one is pretty easy to solve:

#include <Array.au3>

$sText = "Geek - Smarter Shopping 1.1.5" & @CRLF & _
    "Soccer Stars 1.4.3" & @CRLF & _
    "Rock Hero 1.1.3" & @CRLF & _
    "Pinterest 4.6.1" & @CRLF & _
    "Winter Craft 3: Mine Build 1.1.5" & @CRLF & _
    "Flick Shoot 2 1.25" & @CRLF & _
    "Spider-Man Unlimited 1.4.1a"

$aRet = StringRegExp($sText, "(?mU)^(.*)([\d.]*\D?)$", 3)

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

The only change is this:

([\d.]*\D?) - Capture any group made up of digits and dots (which might have a trailing non-digit) until...

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

 

Share this post


Link to post
Share on other sites
kylomas

Alexxander,

Another possible solution...

#include <Array.au3>

$sText = "Geek - Smarter Shopping 1.1.5" & @CRLF & _
    "Soccer Stars 1.4.3" & @CRLF & _
    "Rock Hero 1.1.3" & @CRLF & _
    "Pinterest 4.6.1" & @CRLF & _
    "Winter Craft 3: Mine Build 1.1.5" & @CRLF & _
    "Flick Shoot 2 1.25" & @CRLF & _
    "Spider-Man Unlimited 1.4.1a"

$aRet = StringRegExp($sText, "(?m)[^ ]+$", 3)

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

kylomas

edit: Ooops, created another entry in thread instead of edit...

Edited by kylomas
additional info

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

or this...

#include <Array.au3>

$sText = "Geek - Smarter Shopping 1.1.5" & @CRLF & _
    "Soccer Stars 1.4.3" & @CRLF & _
    "Rock Hero 1.1.3" & @CRLF & _
    "Pinterest 4.6.1" & @CRLF & _
    "Winter Craft 3: Mine Build 1.1.5" & @CRLF & _
    "Flick Shoot 2 1.25" & @CRLF & _
    "Spider-Man Unlimited 1.4.1a"

$aRet = StringRegExp($sText, "(?m)[\w|\.]+$", 3)

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

 


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
mikell

Melba,

Your code will fail if an item has no version number

$sText = "Geek - Smarter Shopping 1.1.5" & @CRLF & _
    "Soccer Stars 1.4.3" & @CRLF & _
    "Rock Hero 1.1.3" & @CRLF & _
    "Tralala" & @CRLF & _
    "Pinterest 4.6.1" & @CRLF & _
    "Winter Craft 3: Mine Build 1.1.5" & @CRLF & _
    "Flick Shoot 2 1.25" & @CRLF & _
    "Spider-Man Unlimited 1.4.1a"

So you could use

$txt2 = StringRegExpReplace($sText, '\h(?=\d+\.)', "#")
Msgbox(0,"", $txt2)

And then securely StringSplit each item  :)

Share this post


Link to post
Share on other sites
kylomas

mikell - Good catch - mine will fail also!  But now we are getting into the semantics of "what is a version number"...

Edited by kylomas
clarification

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
Alexxander

thank you all for responces 

 

what do you think about my Modest method ?

$file[12] = Spider-Man Unlimited 1.4.1a
$split = StringInStr($file[12],".") - 2         ;name + version
$name = StringMid($file[12],1,$split)
$version = StringMid($file[12],$split + 1,-1)
MsgBox(0,0,"'" & $name & "_" & $version & "'")

 


Share this post


Link to post
Share on other sites
kylomas

Alexxander,

It's OK if the spacing never changes...consider the following (similar to yours)

#include <Array.au3>

$sText = "Geek - Smarter Shopping 1.1.5" & @CRLF & _
    "Soccer Stars 1.4.3" & @CRLF & _
    "Rock Hero 1.1.3" & @CRLF & _
    "Pinterest" & @CRLF & _
    "Winter Craft 3: Mine Build 1.1.5" & @CRLF & _
    "Flick Shoot 2 1.25" & @CRLF & _
    "Spider-Man Unlimited 1.4.1a"

local $aText = stringsplit($sText,@CRLF,$STR_NOCOUNT+$STR_ENTIRESPLIT)

for $i = 0 to ubound($aText) - 1
    $aRet = StringRegExp($aText[$i], "(?m)(.+) +(.+)$", 3)
    if not isarray($aRet) then
        ConsoleWrite('Product = ' & $aText[$i] & @CRLF)
    Else
        ConsoleWrite(stringformat('Product = %-30s  Version = %10s', $aRet[0], $aRet[1]) & @CRLF)
    endif

next

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
mikell

kylomas,
Let's assume that the 2 is a version number in "Flick Shoot 2.0" but not in "Flick Shoot 2" 
:D

Alexxander,
Considering the title of this topic, please note that my previous example gives the same result if you just replace # by _ in the expression

Share this post


Link to post
Share on other sites
kylomas

mikell - Truly, then the OP can change it to whatever he likes...again "what is a version number"...

 


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
mikell

kylomas,
Definitely I'd do it like this for a result in a 2D array

#include <Array.au3>

$sText = "Geek - Smarter Shopping 1.1.5" & @CRLF & _
    "Soccer Stars 1.4.3" & @CRLF & _
    "Rock Hero 1.1.3" & @CRLF & _
    "Tra la la" & @CRLF & _
    "Tra la la 2" & @CRLF & _
    "Pinterest 4.6.1" & @CRLF & _
    "Winter Craft 3: Mine Build 1.1.5" & @CRLF & _
    "Flick Shoot 2 1.25" & @CRLF & _
    "Spider-Man Unlimited 1.4.1a"

$txt2 = StringRegExpReplace($sText, '\h(?=\d+\.)', "#")
;Msgbox(0,"", $txt2)

$lines = StringRegExp($txt2, '(?m)(^.*)\R?', 3)
$n = UBound($lines)
Local $res[$n][2]
For $i = 0 to $n - 1
   $s = StringSplit($lines[$i], "#")
   $res[$i][0] = $s[1]
   $res[$i][1] = ($s[0] = 1) ? "" : $s[2]
Next
_ArrayDisplay($res)

Edit
A version number should include a dot - otherwise nothing is possible  :)

Edited by mikell

Share this post


Link to post
Share on other sites
kylomas

@mikell - Nice Solution!


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
mikell

Thanks
It should work in any case - until the next unexpected requirement  ;)

Share this post


Link to post
Share on other sites
Melba23

mikell,

My version (I had not seen yours beforehand):

#include <Array.au3>

$sText = "Geek - Smarter Shopping 1.1.5" & @CRLF & _
    "Soccer Stars 1.4.3" & @CRLF & _
    "Rock Hero 1.1.3" & @CRLF & _
    "Pinterest 4.6.1" & @CRLF & _
    "No Version Number" & @CRLF & _
    "Winter Craft 3: Mine Build 1.1.5" & @CRLF & _
    "Flick Shoot 2 1.25" & @CRLF & _
    "Spider-Man Unlimited 1.4.1a"

$aRet = StringRegExp($sText, "(?mU)^(.*)([\d.]+\D?)?$", 3)

Global $aFinal[UBound($aRet)][2]

$iIndex = -1
For $i = 0 To UBound($aRet) - 1 Step 2
    $iIndex +=1
    $aFinal[$iIndex][0] = $aRet[$i]
    ConsoleWrite($aRet[$i] & " - " & StringLeft($aRet[$i + 1], 1) & @CRLF)
    If StringRegExp(StringLeft($aRet[$i + 1], 1), "\d") Then
        $aFinal[$iIndex][1] = $aRet[$i + 1]
    Else
        $i -= 1
    EndIf

Next

ReDim $aFinal[$iIndex][2]
_ArrayDisplay($aFinal, $i, 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 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

 

Share this post


Link to post
Share on other sites
Alexxander

What i like about this forum members , that other forums members does not have is that they are always searching for something to stay busy :D

 

thank you guys i really love this place , here is where i learned the first line of code

All respect 

  • Like 1

Share this post


Link to post
Share on other sites
mikell

Melba,

"Spider-Man Unlimited 1.4.1a2"

Oy oy oy  :)

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

×