Rex

[Solved ]Stringregex problem, return multiply results

13 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

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

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

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

Share this post


Link to post
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?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Glad to help. :)

Share this post


Link to post
Share on other sites
59 minutes ago, Rex said:

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

I don't agree  :)

$aRet = StringRegExp($sString, '(\w+)\.lst', 3)

 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

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

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

Share this post


Link to post
Share on other sites

#10 ·  Posted

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

Share this post


Link to post
Share on other sites

#11 ·  Posted

On 30/4/2016 at 6:46 PM, mikell said:

I don't agree  :)

$aRet = StringRegExp($sString, '(\w+)\.lst', 3)

 

Yes, but with my code - the .lst was the problem :)

Cheers
/Rex

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#13 ·  Posted

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

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