AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Regex help in breaking a line into subpattern

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
twhyman



Joined: 07 Dec 2005
Posts: 218

PostPosted: Tue May 15, 2007 8:21 am    Post subject: Regex help in breaking a line into subpattern Reply with quote

Hi,

i want to capture and store hard drive s.m.a.r.t data into subpatterns

i tried using an array but it didnt work universally for every line.

Code:

ID   Attribute                  Type  Threshold Value Worst        Raw Status
---- -------------------------- ----- --------- ----- ----- ---------- --------
[01] Raw Read Error Rate        Prefailure   51   200   200         11 OK
[03] Spin Up Time               Prefailure   21   188   187       1558 OK
[04] Start/Stop Count           Advisory      0   100   100        673 OK
[05] Reallocated Sector Count   Prefailure  140   200   200          0 OK
[07] Seek Error Rate            Advisory      0   200   200          0 OK
[09] Power On Hours Count       Advisory      0    99    99       1202 OK
[0A] Spin Retry Count           Advisory      0   100   100          0 OK
[0B] Calibration Retry Count    Advisory      0   100   100          0 OK
[0C] Power Cycle Count          Advisory      0   100   100        554 OK
[C0] Power-Off Park Count       Advisory      0   200   200        451 OK
[C1] Load/Unload Cycle Count    Advisory      0   190   190      31415 OK
[C2] Drive Temperature          Advisory      0   110   105         37 OK
[C4] Re-Allocated Data Count    Advisory      0   200   200          0 OK
[C5] Pending Sector Count       Advisory      0   200   200          0 OK
[C6] UnCorrectable Sector Count Advisory      0   200   200          0 OK
[C7] CRC Error Count            Advisory      0   200   200          0 OK
[C8] Write Error Rate           Advisory      0   200   200          0 OK


thanks
Twhyman
Back to top
View user's profile Send private message
Helpy
Guest





PostPosted: Tue May 15, 2007 9:34 am    Post subject: Reply with quote

Since your data seems to be well aligned, I believe parsing with SubStr is an easy way.
Otherwise, you can use stuff like: ".{4} .{50}" and so on, to get the split in one go.
Back to top
twhyman



Joined: 07 Dec 2005
Posts: 218

PostPosted: Sat May 19, 2007 7:32 am    Post subject: Reply with quote

helpy,

can you give an example cause i dont realy follow you?

thanks
twhyman
Back to top
View user's profile Send private message
atnbueno



Joined: 24 Mar 2007
Posts: 26

PostPosted: Sat May 19, 2007 10:14 am    Post subject: Reply with quote

Hi,

Helpy means that you can use the fact that every part of your data can be considered to start at a particular column/position. Discarding the header rows, your data follows this pattern:
ID: starts at position 2, has a length of 2
Attribute: starts at position 6, has a length of 26
Type: starts at position 33, has a length of 10
Threshold: starts at position 44, has a length of 4
Value: starts at position 49, has a length of 5
Worst: starts at position 55, has a length of 5
Raw: starts at position 61, has a length of 10
Status: starts at position 72, has a length of 8

Each one of these can be directly translated into a SubStr() or a StringMid command. Then you'll have to deal with the spaces left and right.

On the other side, a RegEx would allow you to process each line with a single command (including the space removal).
_________________
Regards,
Antonio
Back to top
View user's profile Send private message Visit poster's website
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Sat May 19, 2007 11:51 am    Post subject: Reply with quote

atnbueno wrote:
Then you'll have to deal with the spaces left and right.
Not if he uses StringMid with AutoTrim on (the default).
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
atnbueno



Joined: 24 Mar 2007
Posts: 26

PostPosted: Sat May 19, 2007 12:50 pm    Post subject: Reply with quote

You're right. I forgot about AutoTrim.
_________________
Regards,
Antonio
Back to top
View user's profile Send private message Visit poster's website
twhyman



Joined: 07 Dec 2005
Posts: 218

PostPosted: Sat May 19, 2007 4:23 pm    Post subject: Reply with quote

thanks for the help guys, but i am looking for a single regex line to procces the lines.

i tried for hours to get it but with no succes Sad

can you help me with the regex (maybe PhiLho the RegEx master Smile)?

Thanks
Twhyman
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Sat May 19, 2007 5:45 pm    Post subject: Reply with quote

OK, it isn't so hard, just need to be clean:
Code:
lines =
(
ID   Attribute                  Type  Threshold Value Worst        Raw Status
---- -------------------------- ----- --------- ----- ----- ---------- --------
[01] Raw Read Error Rate        Prefailure   51   200   200         11 OK
[03] Spin Up Time               Prefailure   21   188   187       1558 OK
[04] Start/Stop Count           Advisory      0   100   100        673 OK
[05] Reallocated Sector Count   Prefailure  140   200   200          0 OK
[07] Seek Error Rate            Advisory      0   200   200          0 OK
[09] Power On Hours Count       Advisory      0    99    99       1202 OK
[0A] Spin Retry Count           Advisory      0   100   100          0 OK
[0B] Calibration Retry Count    Advisory      0   100   100          0 OK
[0C] Power Cycle Count          Advisory      0   100   100        554 OK
[C0] Power-Off Park Count       Advisory      0   200   200        451 OK
[C1] Load/Unload Cycle Count    Advisory      0   190   190      31415 OK
[C2] Drive Temperature          Advisory      0   110   105         37 OK
[C4] Re-Allocated Data Count    Advisory      0   200   200          0 OK
[C5] Pending Sector Count       Advisory      0   200   200          0 OK
[C6] UnCorrectable Sector Count Advisory      0   200   200          0 OK
[C7] CRC Error Count            Advisory      0   200   200          0 OK
[C8] Write Error Rate           Advisory      0   200   200          0 OK
)
Loop Parse, lines, `n, `r
{
   fp := RegExMatch(A_LoopField, "\[(?'ID'..)]\s(?'Attribute'.{27})(?'Type'\w+)\s+(?'Threshold'\d+)\s+(?'Value'\d+)\s+(?'Worst'\d+)\s+(?'Raw'\d+)\s+(?'Status'\w+)", field)
   If (fp > 0)
   {
      fieldAttribute = %fieldAttribute% ; Trim spaces
      MsgBox ID: %fieldID% Attribute: %fieldAttribute% Type: %fieldType% Threshold: %fieldThreshold% Value: %fieldValue% Worst: %fieldWorst% Raw: %fieldRaw% Status: %fieldStatus%
   }
}
[EDIT] Took in account atnbueno's remarks.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Last edited by PhiLho on Sat May 19, 2007 8:32 pm; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website
atnbueno



Joined: 24 Mar 2007
Posts: 26

PostPosted: Sat May 19, 2007 7:31 pm    Post subject: Reply with quote

There's a
Code:
(?'Threshold'\d+)\s+

missing in the RegEx and I would left the square brackets outside the ID Smile but other than that, thanks for the code, a very clarifying example.

I hadn't noticed the problem with "Attribute" and the spaces Embarassed
_________________
Regards,
Antonio
Back to top
View user's profile Send private message Visit poster's website
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Sat May 19, 2007 8:33 pm    Post subject: Reply with quote

Good remarks, thanks. I corrected the code accordingly.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
twhyman



Joined: 07 Dec 2005
Posts: 218

PostPosted: Sun May 20, 2007 2:04 am    Post subject: Reply with quote

thanks alot guys for the help Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group