Alexxander Posted October 7, 2013 Share Posted October 7, 2013 hi all i have a lot of txt files like this {1} Blessed be He in Whose hands is the Dominion; and He has Power over all things; {2} It is He Who created Death and Life, that He might put you to the test which of you is best in deed: And He is the Exalted in Might, the Oft-Forgiving; {3} It is He Who created the seven heavens one above another: No want of proportion will you see in the Creation of Allah the Most Gracious. So turn your sight again: Do you see any flaw? i want to make a script that can mark and cut each number with its sentence and put it in a .txt file i think i must use StringInStr function the problem is i want it to detect brackets that have numbers in ithe number in between the brackets {} then copy the text till it reach the next bracket {} without copying the end bracket so basically i want to split the long text to smaller text and the brackets will be the border so the first one will be {1} Blessed be He in Whose hands is the Dominion; and He has Power over all things; if the script faces a bracket without a number in it it must ignore it , the bracket must have a number in it how can i tell autoit to select from the fisrst {number} to before the next {number} ? Link to comment Share on other sites More sharing options...
mrflibblehat Posted October 7, 2013 Share Posted October 7, 2013 (edited) Something like this. #include <Array.au3> $vString = "{1} Blessed be He in Whose hands is theDominion; and He has Power over all things; {2} It is He Who created Death and Life, that He might put you to the test which of you is best in deed: And He is the Exalted in Might, the Oft-Forgiving; {3} It is He Who created the seven heavens one above another: No want of proportion will you see in the Creation of Allah the Most Gracious. So turn your sight again: Do you see any flaw?; " $vXtract = StringRegExp($vString, '{\d}\h+(.*?);', 3) _ArrayDisplay($vXtract) Edited October 7, 2013 by mrflibblehat Alexxander 1 [font="'courier new', courier, monospace;"]Pastebin UDF | Prowl UDF[/font] Link to comment Share on other sites More sharing options...
Alexxander Posted October 7, 2013 Author Share Posted October 7, 2013 Something like this. #include <Array.au3> $vString = "{1} Blessed be He in Whose hands is theDominion; and He has Power over all things; {2} It is He Who created Death and Life, that He might put you to the test which of you is best in deed: And He is the Exalted in Might, the Oft-Forgiving; {3} It is He Who created the seven heavens one above another: No want of proportion will you see in the Creation of Allah the Most Gracious. So turn your sight again: Do you see any flaw?; " $vXtract = StringRegExp($vString, '{\d}(.*?);', 3) _ArrayDisplay($vXtract) Thank you bro this is so much use full i really appreciate your help Link to comment Share on other sites More sharing options...
rodent1 Posted October 7, 2013 Share Posted October 7, 2013 that won't preserve the "{1}" part, here's another way: #include <array.au3> Local $sIn = "{1} text 1; {2} text 2; {3} blah blah 3." Main() Func Main() Local $arSIn = StringSplit($sIn, "{") Local $NewSIn for $i = 1 to $arSIn[0] if $arSIn[$i]*1>0 Then $NewSIn &= "~~{" & $arSIn[$i] Else if stringlen($arSIn[$i]) Then $NewSIn &= "{" & $arSIn[$i] EndIf Next if StringLeft($NewSIn, 3)="~~{" Then $NewSIn = StringRight($NewSIn, StringLen($NewSIn)-2) $arSIn = StringSplit($NewSIn, "~~", 1) _ArrayDisplay($arSIn) EndFunc I'm sure there is a way using regular expressions, but this will do the job Alexxander 1 Link to comment Share on other sites More sharing options...
Solution sahsanu Posted October 7, 2013 Solution Share Posted October 7, 2013 Just another approach: #include <Array.au3> $sString="{1} Blessed be He in Whose hands is the Dominion; and He has Power over all things; {2} It is He Who created Death" & _ "and Life, that He might put you to the test which of you is best in deed: And He is the Exalted in Might, the Oft-Forgiving;" & _ "{3} It is He Who created the seven heavens one above another: No want of proportion will you see in the Creation of Allah the" & _ "Most Gracious. So turn your sight again: Do you see any flaw? " $sResult=StringRegExpReplace($sString,"(\{\d{1,}\})",@CR & "$1") $sResult=StringRegExpReplace($sResult,"(\r)(.*)","$2",1) ConsoleWrite('Result:' & @CRLF & $sResult & @CRLF) ;If you want it in an array add this: $aArray=StringSplit($sResult,@CR) _ArrayDisplay($aArray) Cheers, sahsanu Alexxander 1 Link to comment Share on other sites More sharing options...
kylomas Posted October 7, 2013 Share Posted October 7, 2013 Alexxander, Thank you bro this is so much use full If you want to use fibblehat's SRE and get the numbers just move the start of the capturing group like this... #include <Array.au3> #include <userudfs.au3> $vString = "{1} Blessed be He in Whose hands is theDominion; and He has Power over all things; {2} It is He Who created Death and Life, that He might put you to the test which of you is best in deed: And He is the Exalted in Might, the Oft-Forgiving; {3} It is He Who created the seven heavens one above another: No want of proportion will you see in the Creation of Allah the Most Gracious. So turn your sight again: Do you see any flaw?; " $vXtract = StringRegExp($vString, '({\d}\h+.*?);', 3) _ad($vXtract) kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
sahsanu Posted October 7, 2013 Share Posted October 7, 2013 (edited) Just one thing , that will solve the {number} issue but will not get what the OP needs, it will break the first sentence because of that sentence has 2 ";" (the problem is in the original regexp too). I mean, it will return: {1} Blessed be He in Whose hands is theDominion; Instead of: {1} Blessed be He in Whose hands is theDominion; and He has Power over all things; Cheers, sahsanu Edited October 7, 2013 by sahsanu Link to comment Share on other sites More sharing options...
Malkey Posted October 7, 2013 Share Posted October 7, 2013 ....i want to make a script that can mark and cut each number with its sentence and put it in a .txt file .... This example splits a string into sub-strings based on the opening curly bracket character "{". Local $sText = "{1} Blessed are the cheese makers; and He has 1 Power " & _ "over all things; {2} It is Dr Who created Death and Life" & _ ", that He is so huge; {3} It is Dr Who created the seven" & _ " heavens one above another and side by side and one within another; " Local $sSegmentedText = StringRegExpReplace($sText, '({\d+}[^{]+)', "\1" & @CRLF) ConsoleWrite($sSegmentedText & @LF) Link to comment Share on other sites More sharing options...
kylomas Posted October 8, 2013 Share Posted October 8, 2013 @sahsanu - Thanks...missed that... @Malkey - "Dr Who"...that is hillarious. Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
Alexxander Posted October 8, 2013 Author Share Posted October 8, 2013 (edited) Thank you all for your nice replays anyways this code is from the help file Local $text = "This\nline\ncontains\nC-style breaks." Local $array = StringSplit($text, '\n', 1) if i put msgbox(0,0, $array) at the end of that code why i am not able to get the split ed words of the message box ? i cant even get them in console write or file write it is empty ? Edited October 8, 2013 by Alexxander Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 8, 2013 Moderators Share Posted October 8, 2013 Alexxander,It is because you can only display variables or single array elements in a MsgBox - to see them all you need to use _ArrayDisplay:#include <Constants.au3> #include <Array.au3> Local $text = "This\nline\ncontains\nC-style breaks." Local $array = StringSplit($text, '\n', 1) ; The whole array _ArrayDisplay($array) ; Each element in turn For $i = 01 To $array[0] MsgBox($MB_SYSTEMMODAL, "Element: " & $i, $array[$i]) NextAll clear? M23 Alexxander 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Alexxander Posted October 8, 2013 Author Share Posted October 8, 2013 Melba23 thank you so much this forum and it's members are awesome Link to comment Share on other sites More sharing options...
Jury Posted October 12, 2013 Share Posted October 12, 2013 Why not just use positive lookahead (?={) #include <Array.au3> $sString="{1} Blessed be He in Whose hands is the Dominion; and He has Power over all things; {2} It is He Who created Death" & _ "and Life, that He might put you to the test which of you is best in deed: And He is the Exalted in Might, the Oft-Forgiving;" & _ "{3} It is He Who created the seven heavens one above another: No want of proportion will you see in the Creation of Allah the" & _ "Most Gracious. So turn your sight again: Do you see any flaw? " $aArray=StringRegExp($sString,"(\{\d+.*?)(?=\{|\Z)",3) _ArrayDisplay($aArray) sahsanu 1 Link to comment Share on other sites More sharing options...
sahsanu Posted October 12, 2013 Share Posted October 12, 2013 Why not just use positive lookahead (?={) Jury, great solution. In my opinion this is the best answer ;-) 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