dmob Posted October 13, 2015 Share Posted October 13, 2015 I have these lines:2.1. This is 1st line2.2. This is 2nd line [5,4]4.2.1 This is 3rd line [5.5]4.2.2. This is 4th line [5½]I need to extract the line number, the line text itself and the number(s) in square brackets.My problem is with lines 2.1 and 2.2.... I can extract one or the other but I am battling to get bothThis is the regex I am fumbling with:(\d)\.?(\d)?\.?(\d)?\.?\s(.*)\[(.*)\]I have spent hours messing around and would appreciate help getting it right. Link to comment Share on other sites More sharing options...
jchd Posted October 13, 2015 Share Posted October 13, 2015 Does this work for you?#include <Array.au3> Local $s = _ "2.1. This is 1st line" & @CRLF & _ "2.2. This is 2nd line [5,4]" & @CRLF & _ "4.2.1 This is 3rd line [5.5]" & @CRLF & _ "4.2.2. This is 4th line [5½]" Local $a = StringRegExp($s, "(?m)^((?:\d+\.?)+)\h+([^\[\v]+)(?|\h+\[([^\]]+)\]|())$", 3) _ArrayDisplay($a) 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
dmob Posted October 13, 2015 Author Share Posted October 13, 2015 Blimey!! Yes it works!I was hoping to get the line numbers split separately, but I can split them from the result array.Thank you so much. Link to comment Share on other sites More sharing options...
jchd Posted October 13, 2015 Share Posted October 13, 2015 That's possible using a regexp but requires a fixed maximum number of captures setup in the pattern and unused captures will get useless blank stings. To keep flexible you have better time handling that in aftermath code, just like removing the final extra dot in 2.1.3. 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
dmob Posted October 13, 2015 Author Share Posted October 13, 2015 Yes thanks jhcd, and I will also try to wrap my head around your regex Thank you for your time and help Link to comment Share on other sites More sharing options...
jchd Posted October 13, 2015 Share Posted October 13, 2015 (edited) http://regex101.com/ will tell you all you want. Set option g (global) which is the equivalent of our option 3. Edited October 13, 2015 by jchd 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
mikell Posted October 13, 2015 Share Posted October 13, 2015 #include <Array.au3> Local $s = _ "2.1. This is 1st line" & @CRLF & _ "2.2. This is 2nd line [5,4]" & @CRLF & _ "4.2.1 This is 3rd line [5.5]" & @CRLF & _ "4.2.2. This is 4th line [5½]" Local $a1 = StringRegExp($s, "(?m)^((?:\d+\.?)+)\h+([^\[\v]+)(?|\h+\[([^\]]+)\]|())$", 3) $a2 = _ArrayDiv($a1, 3) _ArrayDisplay($a2) ;==================================== Func _ArrayDiv($array, $div, $cols = $div) If Mod(UBound($array), $div) <> 0 Then Return SetError(1, 0, 0) If $cols < $div Then Return SetError(2, 0, 0) Local $tmp[UBound($array)/$div][$cols] For $i = 0 to UBound($array)-1 step $div For $j = 0 to $div-1 $tmp[$i/$div][$j] = $array[$i+$j] Next Next Return $tmp EndFunc dmob 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now