Sign in to follow this  
Followers 0
Alexxander

Strings question ...

14 posts in this topic

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} ?


Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by mrflibblehat
1 person likes this

[font="'courier new', courier, monospace;"]Pastebin UDF | Prowl UDF[/font]

Share this post


Link to post
Share on other sites

 

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


Share this post


Link to post
Share on other sites

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

1 person likes this

Share this post


Link to post
Share on other sites

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

1 person likes this

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#7 ·  Posted (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 by sahsanu

Share this post


Link to post
Share on other sites

....

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)

Share this post


Link to post
Share on other sites

@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

Share this post


Link to post
Share on other sites

#10 ·  Posted (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 by Alexxander

Share this post


Link to post
Share on other sites

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])
Next
All clear? :)

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Melba23

 

thank you so much 

this forum and it's members are awesome 


Share this post


Link to post
Share on other sites

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)
1 person likes this

Share this post


Link to post
Share on other sites

 

Why not just use positive lookahead  (?={)

 

Jury, great solution. In my opinion this is the best answer ;-)

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