Sign in to follow this  
Followers 0
davezub

SRE Help

5 posts in this topic

So I'm trying to use SRE and I'm not getting too far. Here is the line of text:

233 GB Total 42.99 GB (18%) Free 24% Fragmented (49% file fragmentation)

I want to grab just the % amounts. So I grab a copy of SRE tester, read the Autoit help, google some examples, smack my head, etc.. So I found if I use (\d\d%) with global matches it pulls all three amounts. However, in testing I noticed that if the % amount is a single digit (e.g., 8%) its does not work. So I'm wondering can SRE be used to conditionally find the % amounts. I'm writing a script to obtain the fragmentation amounts for a drive to place into LANDesk.

Thanks.

Share this post


Link to post
Share on other sites



So I'm trying to use SRE and I'm not getting too far. Here is the line of text:

233 GB Total 42.99 GB (18%) Free 24% Fragmented (49% file fragmentation)

I want to grab just the % amounts. So I grab a copy of SRE tester, read the Autoit help, google some examples, smack my head, etc.. So I found if I use (\d\d%) with global matches it pulls all three amounts. However, in testing I noticed that if the % amount is a single digit (e.g., 8%) its does not work. So I'm wondering can SRE be used to conditionally find the % amounts. I'm writing a script to obtain the fragmentation amounts for a drive to place into LANDesk.

Thanks.

Like this?

$string = "233 GB Total 42.99 GB (18%) Free 24% Fragmented (49% file fragmentation)"
If StringRegExp ($string, "[0-9]{1,2}%", 0) Then
    $RegExp = StringRegExp ($string, "([0-9]{1,2})(?:%)", 3)
    For $x = 0 to UBound($RegExp)-1
        MsgBox (0, "", $RegExp[$x])
    Next
EndIf

Bear in mind that with Regular Expressions there's usually at least 3 different ways to do something, but this one seems to work well enough for what you want to do.

Share this post


Link to post
Share on other sites

Like this?

$string = "233 GB Total 42.99 GB (18%) Free 24% Fragmented (49% file fragmentation)"
If StringRegExp ($string, "[0-9]{1,2}%", 0) Then
    $RegExp = StringRegExp ($string, "([0-9]{1,2})(?:%)", 3)
    For $x = 0 to UBound($RegExp)-1
        MsgBox (0, "", $RegExp[$x])
    Next
EndIf

Bear in mind that with Regular Expressions there's usually at least 3 different ways to do something, but this one seems to work well enough for what you want to do.

Like you said, there are many ways. I prefer to run the SRE once and just test for @error:
#include <Array.au3>

$string = "233 GB Total 42.99 GB (18%) Free 24% Fragmented (49% file fragmentation)"
$RegExp = StringRegExp ($string, "(\d+)(?:%)", 3)
If Not @error Then _ArrayDisplay($RegExp, "Results")

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Outstanding. Not only does it work but now I understand how it works.

Thanks.

Share this post


Link to post
Share on other sites

Outstanding. Not only does it work but now I understand how it works.

Thanks.

I don't think we could hope for any better. :) Good deal!

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
Sign in to follow this  
Followers 0