Jump to content

Regex expression


Recommended Posts

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

Link to comment
Share on other sites

  • Moderators

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

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 comment
Share on other sites

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 ?

Link to comment
Share on other sites

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)

 

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

Link to comment
Share on other sites

  • Moderators

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 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 comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Moderators

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