Jump to content

[Solved ]Stringregex problem, return multiply results


Rex
 Share

Recommended Posts

Hi I have a string like this

User IDs: User001, User002, User0003, user0004

And I want to return all user ID's using stringregexp, but I can only get it to return the last string eg. user0004

_ArrayDisplay(StringRegExp($String, '(user\d{1,})', 1))

I have tried to uses \K \1 (?1) but none of them helped, I can only get it to return the last string :(

I have been reading the help file forth and back a million times now, I goggled stringregexp and still I can't get it to return all strings.

Could it be that there is a stringregexp expert in here that could help me get the result I want?

 

Cheers
/Rex

Edited by Rex
Link to comment
Share on other sites

  • Moderators

Rex,

Set the flag to 3 ($STR_REGEXPARRAYGLOBALMATCH) rather than 1 and make the match case-insensitive:

#include <Array.au3>

$sString = "User IDs: User001, User002, User0003, user0004"

$aRet = StringRegExp($sString, "(?i)(user\d{1,})", 3)

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

M23

Edit: Good for MichaelHB!

Edited by Melba23

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

Cool :)

The caseless is not necessary, it was a typo from my side, forgot to upper the last U in my test string :>

This works like a charm on my test string, but I can't get it to work on actual string :/ witch of course I should had posted instead of the test string >_<

We get this result from our server.

{"Date":"20160422","Flow":[{"Ordres":[{"Ordre":2985484,"path":"\\emp\work\ordres\2985484.log"},{"Ordre":2954269,"path":"\\emp\work\ordres\2954269.log"},{"Ordre":2986125,"path":"\\emp\work\ordres\2986125.log"}],"ended":"true"}],"lists":[{"list":[{"path":"\\emp\work\user582.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user580.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user492.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user495.lst"}],"ended":"true"}]}

and it's the user list path I'm trying to get using regexp

_ArrayDisplay(StringRegExp($string, '.*"(.*\.lst).*', 1))

 

Cheers
/Rex

Link to comment
Share on other sites

Let's use Melba's example (its more organized than mine :D).

#include <Array.au3>

$sString = '{"Date":"20160422","Flow":[{"Ordres":[{"Ordre":2985484,"path":"\\emp\work\ordres\2985484.log"},{"Ordre":2954269,"path":"\\emp\work\ordres\2954269.log"},{"Ordre":2986125,"path":"\\emp\work\ordres\2986125.log"}],"ended":"true"}],"lists":[{"list":[{"path":"\\emp\work\user582.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user580.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user492.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user495.lst"}],"ended":"true"}]}'

$aRet = StringRegExp($sString, '(?i)"path":"(.*?)"}]', 3)

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

Exit

Is this the result that you want?

Link to comment
Share on other sites

@MichaelHB

Damn, that's nice :) that's exactly what I was trying to get :)

It seems that the problem was that I was checking for ".lst" but I shouldn't.

Thx so much, I had ben struggling with that for 2-3 hours last night - until I gave up and went to bed.

 

Cheers
/Rex

Link to comment
Share on other sites

i don't not agree, but for different reasons

$aRet = StringRegExp($sString, '(user\d+?)\.', 3)
$sString = '{"Date":"20160422","Flow":[{"Ordres":[{"Ordre":2985484,"path":"\\emp\work\ordres\2985484.log"},{"Ordre":2954269,"path":"\\emp\work\ordres\2954269.log"},{"Ordre":2986125,"path":"\\emp\work\ordres\2986125.log"}],"ended":"true"}],"lists":[{"list":[{"path":"\\emp\work\user582.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user580.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user492.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user495.lst"}],"ended":"true"}]}'
$sRet = ""
$i = 0

Do
$i += 1
$sRet &= stringmid($sString , stringinstr($sString , "user" , 1 , $i) , stringinstr($sString , ".lst" , 1 , $i) - stringinstr($sString , "user" , 1 , $i)) & @CRLF
Until stringinstr($sRet , @CRLF & @CRLF)

msgbox(0, $i , stringtrimright($sRet , 4))

 

Edited by iamtheky

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

Link to comment
Share on other sites

Just for fun:
 

#include <Array.au3>
#include <String.au3>

$sString = '{"Date":"20160422","Flow":[{"Ordres":[{"Ordre":2985484,"path":"\\emp\work\ordres\2985484.log"},{"Ordre":2954269,"path":"\\emp\work\ordres\2954269.log"},{"Ordre":2986125,"path":"\\emp\work\ordres\2986125.log"}],"ended":"true"}],"lists":[{"list":[{"path":"\\emp\work\user582.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user580.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user492.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user495.lst"}],"ended":"true"}]}'

$users = _StringBetween($sString,"us",".lst")

For $i = 0 To UBound($users) -1
        $users[$i] = StringReplace($users[$i],"er","user")
Next

_ArrayDisplay($users)

Regards

Link to comment
Share on other sites

20 hours ago, iamtheky said:

i don't not agree, but for different reasons

$aRet = StringRegExp($sString, '(user\d+?)\.', 3)
$sString = '{"Date":"20160422","Flow":[{"Ordres":[{"Ordre":2985484,"path":"\\emp\work\ordres\2985484.log"},{"Ordre":2954269,"path":"\\emp\work\ordres\2954269.log"},{"Ordre":2986125,"path":"\\emp\work\ordres\2986125.log"}],"ended":"true"}],"lists":[{"list":[{"path":"\\emp\work\user582.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user580.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user492.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user495.lst"}],"ended":"true"}]}'
$sRet = ""
$i = 0

Do
$i += 1
$sRet &= stringmid($sString , stringinstr($sString , "user" , 1 , $i) , stringinstr($sString , ".lst" , 1 , $i) - stringinstr($sString , "user" , 1 , $i)) & @CRLF
Until stringinstr($sRet , @CRLF & @CRLF)

msgbox(0, $i , stringtrimright($sRet , 4))

 

Yes, but for the way I had made my code .lst was the problem :)

Always nice to see another way to get the same result :)

Cheers
/Rex

Edited by Rex
Link to comment
Share on other sites

11 hours ago, alien4u said:

Just for fun:
 

#include <Array.au3>
#include <String.au3>

$sString = '{"Date":"20160422","Flow":[{"Ordres":[{"Ordre":2985484,"path":"\\emp\work\ordres\2985484.log"},{"Ordre":2954269,"path":"\\emp\work\ordres\2954269.log"},{"Ordre":2986125,"path":"\\emp\work\ordres\2986125.log"}],"ended":"true"}],"lists":[{"list":[{"path":"\\emp\work\user582.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user580.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user492.lst"}],"ended":"true"},{"list":[{"path":"\\emp\work\user495.lst"}],"ended":"true"}]}'

$users = _StringBetween($sString,"us",".lst")

For $i = 0 To UBound($users) -1
        $users[$i] = StringReplace($users[$i],"er","user")
Next

_ArrayDisplay($users)

Regards

Cool, It's always to see other ways to get the same result :)

Cheers
/Rex

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