Jump to content

Filter file name with StringRegExp?


Recommended Posts

I want to get the file name that says _stripped.au3 at the end of the file
I can't get the file name.

testV5.1_autoit_stripped.au3
testau3_test.au3
dadasdasd.au3
autoit_autoit.au3
test_stripped.txt

#include <File.au3>

$path = @ScriptDir
$filter = "*.au3"

$search = _FileListToArray($path, $filter)
If IsArray($search) Then
    For $i = 1 To $search[0]
        $RegExp = StringRegExp($search[$i], '(?i)([^_]+)_stripped.au3', 3)
        If IsArray($RegExp) Then
            ConsoleWrite($RegExp[0] & @CRLF)
        EndIf
    Next
EndIf

 

Edited by youtuber
Link to comment
Share on other sites

can you just put it in the filter?

#include <File.au3>
#include<Array.au3>

$path = @ScriptDir
$filter = "*_stripped.au3"

$search = _FileListToArray($path, $filter)

_ArrayDisplay($search)

 

Edited by iamtheky

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

Link to comment
Share on other sites

$path = @ScriptDir
$filter = "*_stripped.au3"

$search = _FileListToArrayRec($path, $filter)

now it uses regex :)

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

Link to comment
Share on other sites

@youtuber

I used  .+  because I assumed that you have no file with the name "_stripped.au3" , but you can use  .*  as well  :)
If you remove parenthesis then you don't remove "_stripped.au3" from the filename

 

8 hours ago, iamtheky said:

now it uses regex

Yes but it again doesn't allow to remove the filter from the filename. Let's say that using this option or not is left to the OPs choice :idiot:

Link to comment
Share on other sites

@iamtheky @mikell

If the file name was this, stripped_v3.au3,stripped_v4.au3,stripped_v5.au3,stripped_v5.1.au3,stripped_v5.2.au3 do you think the following filtering is correct?

$filter = "stripped_v*.au3"
$search = _FileListToArrayRec($path, $filter)

or

$filter = "stripped_v?.au3"
$search = _FileListToArrayRec($path, $filter)

otherwise, would it be more accurate to use regex?

$RegExp = StringRegExp($search[$i], 'stripped_v(\d(.*?){1,2}).au3', 3)

 

Link to comment
Share on other sites

they are all regex, and either can be built to accomodate large buckets of edge cases.  I dont know that there is anywhere to find a preference that isnt pedantic af. 

Edited by iamtheky

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

Link to comment
Share on other sites

Here is a consistent pattern ^_^

Global $Fileversion[6] = ["asdasfasfasd", _
        "stripped_v0.au3", _
        "stripped_v3.au3", _
        "stripped_v4.au3", _
        "stripped_v5.au3", _
        "stripped_v5.1.0.0.au3"]

For $i = 0 To 5
    $RegExp = StringRegExp($Fileversion[$i], 'stripped_v[\d\.]{0,9}.au3', 3)
    If IsArray($RegExp) Then
        ConsoleWrite($RegExp[0] & @CRLF)
    EndIf
Next

 

Edited by youtuber
Link to comment
Share on other sites

@youtuber

You shouldn't even escape the dot in the character class, since it's automatically taken as literal character; you should instead escape the one outside the character class, before the file extension:

stripped_v[\d.]\.au3
stripped_v[0-9.]\.au3

:)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

38 minutes ago, youtuber said:

Here is a consistent pattern

Consistent ? yes. Efficient ? no. Just test it on this

Global $Fileversion[6] = ["asdasfasfasd", _
        "stripped_v5.au3", _
        "stripped_v......au3", _
        "stripped_v.....5.au3", _
        "stripped_v15.12.10.10.au3", _
        "stripped_v5....au3"]

Here is the clean way

$RegExp = StringRegExp($Fileversion[$i], 'stripped_v\d+(?:\.\d+)*\.au3', 3)

 

Link to comment
Share on other sites

19 minutes ago, mikell said:

StringRegExp($Fileversion[$i], 'stripped_v\d+(?:\.\d+)*\.au3', 3)

That wouldn't work with "stripped_v5.au3.txt".  But this simple "^stripped_v.+\.au3$" is working, no ?

Link to comment
Share on other sites

18 minutes ago, Nine said:

That wouldn't work with "stripped_v5.au3.txt".

Right. It should be :   'stripped_v\d+(?:\.\d+)*\.au3$'   ( trailing $ added)
You perfectly showed that regex is not as simple as it seems  :)

 

18 minutes ago, Nine said:

this simple "^stripped_v.+\.au3$" is working, no ?

this one matches  "stripped_vademecum#;;lol-.au3"  ....

Link to comment
Share on other sites

2 minutes ago, mikell said:

this one matches  "stripped_vademecum#;;lol-.au3"  ....

Ok, I got it.  You want to have a pattern after _v of digits/dot/digits/dot...Nothing else.

Link to comment
Share on other sites

3 minutes ago, mikell said:

Yes. It was not explicit in the OPs requirements but I assumed that what was to be matched was a version number with a correct syntax 

Alright, make sense. But then you still need to add ^ at the beginning otherwise you would get false positive with "#123;456:.stripped_v5.au3"

Link to comment
Share on other sites

I'm sorry to plug my program here but I must, for there is something this hobbyist may have gleamed from its vast reserves if he had known in advance, and therefore not ask this question. Which wastes all our time! That's important!

 

If you look at what I did there, $aRegExSFile, it perfectly matches this situation. 

(((.*)(?=\\)\N)|(\\))(.*?)\z

The only difference is change \z to $, add stripped_v and .au3, and put the .*? in between them.

(((.*)(?=\\)\N)|(\\))(stripped_v)(.*?)(.au3)$

https://www.debuggex.com/r/AwbaT9ONYtDiqO7W

Edited by moosjuice
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...