Jump to content

REGEX help


 Share

Recommended Posts

Hi everybody I would like to have a regex for this file
 
 

; extension = bfb
; cost_per_hour = 0
; g_code_prefix = 5E4669726D776172653A56312E30370A3B205B6D6D5D
;     206D6F64650A4732310A3B206162736F6C757465206D6F64650A4739
;     300A
; fan_off = M107
; z_speed_mm_per_s = 2.5
; bed_offset_x_mm = -35

I want an array like that '>.

I succes to read each line with StringRegExp($Value, "(w+)(?: = )(.+)", 3) but it fail with multiline for g_code_prefix.

Thanks you in advance :)

Edited by Sea
Link to comment
Share on other sites

One way :

#Include <Array.au3>

$sString = "extension = bfb" & @CRLF & _
           "cost_per_hour = 0" & @CRLF & _
           "g_code_prefix = 5E4669726D776172653A56312E30370A3B205B6D6D5D" & @CRLF & _
           "    206D6F64650A4732310A3B206162736F6C757465206D6F64650A4739" & @CRLF & _
           "    300A" & @CRLF & _
           "fan_off = M107" & @CRLF & _
           "z_speed_mm_per_s = 2.5" & @CRLF & _
           "bed_offset_x_mm = -35"
           
$aLines = StringRegExp( StringRegExpReplace($sString, "\R\h+", "") , "\N+", 3)

_ArrayDisplay($aLines)
Link to comment
Share on other sites

Another:

#include <Array.au3>

Local $sInput = _
"; extension = bfb" & @CRLF & _
"; cost_per_hour = 0" & @CRLF & _
"; g_code_prefix = 5E4669726D776172653A56312E30370A3B205B6D6D5D" & @CRLF & _
";     206D6F64650A4732310A3B206162736F6C757465206D6F64650A4739" & @CRLF & _
";     300A" & @CRLF & _
"; fan_off = M107" & @CRLF & _
"; z_speed_mm_per_s = 2.5" & @CRLF & _
"; bed_offset_x_mm = -35" & @CRLF       ; this last line break optional

$sInput = StringRegExpReplace($sInput, "\R;\s{2,}", "")
ConsoleWrite($sInput & @LF)

Local $aOutput = StringRegExp($sInput, "(?mx) ^ ; \s+ (\w+) \s+ = \s+ (.+) $", 3)
If Not @error Then
    Local $aResult[UBound($aOutput) / 2][2]
    For $i = 0 To UBound($aResult) - 1
        $aResult[$i][0] = $aOutput[2 * $i]
        $aResult[$i][1] = $aOutput[2 * $i + 1]
    Next
    _ArrayDisplay($aResult)
Else
    ConsoleWrite("Non-conformal input." & @LF)
EndIf

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

ouups, sorry I didn't copy the ";" at the beginning of each line....

so, another attempt :

#Include <Array.au3>

$sContent = FileRead("file.txt")
$aLines = StringRegExp( StringRegExpReplace($sContent, "(?:\R;\h{2,}|(?<=\n|^);\h+)", "") , "\N+", 3)
_ArrayDisplay($aLines)
Edited by jguinch
Link to comment
Share on other sites

And the leading '; ' ???

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

:o Amazing jchd thanks you a lot.

Local $aOutput = StringRegExp($sInput, "(?mx) ^ ; \s+ (\w+) \s+ = \s+ (.+) $", 3)
can you explain it plz? ^^'

MX => multi lines so new line is determinated by a ; ?

Why $ in the end? :)

Thanks you its really what I need :)

Btw thanks you too jguinch but I prefer the jchd output :P

Link to comment
Share on other sites

This useful site explains it all thusly:

/(?mx) ^ ; \s+ (\w+) \s+ = \s+ (.+) $/

    (?mx) Match the remainder of the pattern with the following options:
        m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
        x modifier: extended. Spaces and text after a # in the pattern are ignored
    ^ assert position at start of a line
    ; matches the character ; literally
    \s+ match any white space character [\r\n\t\f ]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    1st Capturing group (\w+)
        \w+ match any word character [a-zA-Z0-9_]
            Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    \s+ match any white space character [\r\n\t\f ]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    = matches the character = literally
    \s+ match any white space character [\r\n\t\f ]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    2nd Capturing group (.+)
        .+ matches any character (except newline)
            Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    $ assert position at end of a line

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Not only it offers several regex flavors (by default it is PCRE [ = AutoIt] compatible), but you can actually see a pattern at work step by step by hitting "regexp debugger". That's an excellent feature to learn how a pattern backtracks.

Enter g in the option box (right to the pattern box) to mimic our StringRegExp option 3 (global match).

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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