Jump to content

Strings question ...


Go to solution Solved by sahsanu,

Recommended Posts

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

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

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

Link to comment
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

Link to comment
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

Link to comment
Share on other sites

  • Solution

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

Link to comment
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

Link to comment
Share on other sites

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
Link to comment
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)
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
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)
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...