Sign in to follow this  
Followers 0
Alexxander

Cutting a text when {digit} is seen

4 posts in this topic

#1 ·  Posted (edited)

Hi all

i have a big amount of text files that is link this 

 

{3} Ceux qui

comparent leurs femmes
au dos de leurs mères,
puis reviennent sur ce
qu’ils ont dit, doivent
affranchir un esclave
avant d’avoir aucun
contact [conjugal] avec
leur femme. C’est ce dont
on vous exhorte. Et Allah est Parfaitement Connaisseur de ce que vous faites. {4} Mais
celui qui n’en trouve pas les moyens doit jeûner alors deux mois consécutifs avant d’avoir
aucun contact [conjugal] avec sa femme. Mais s’il ne peut le faire non plus, alors qu’il
nourrisse soixante pauvres. Cela, pour que vous croyiez en Allah et en Son messager.
Voilà les limites imposées par Allah. Et les mécréants auront un châtiment douloureux.

 

 

i want it to cut each part alone and the separator is bracket then number then bracket {NUM}

so the first one must be 

 

{3} Ceux qui

comparent leurs femmes
au dos de leurs mères,
puis reviennent sur ce
qu’ils ont dit, doivent
affranchir un esclave
avant d’avoir aucun
contact [conjugal] avec
leur femme. C’est ce dont
on vous exhorte. Et Allah est Parfaitement Connaisseur de ce que vous faites. 
 
 
 
i tried this 
 
 
#include <Array.au3>

$sString=ClipGet()

$sResult=StringRegExpReplace($sString,"(\{\d{1,}\})",@CR & "$1")
$sResult=StringRegExpReplace($sResult,"(\r)(.*)","$2",1)

;If you want it in an array add this:
$array=StringSplit($sResult,@CR)

$var = 1
For $i = 1 To $array[0]
     FileWrite(@DesktopDir & "\sura\" & $var & ".txt" , $array[$i] & @CRLF)
$var = $var + 1
Next
 
it is cutting the text when it see the {NUM} but it is also cutting it when it see a new line
i want it to complete the sentence even if it is on 2 lines 
any ideas ?
Edited by Alexxander

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You can't use _StringBetween?

I have missunderstood :D

See if this is useful:

#include <Array.au3>

$sString = ClipGet()
$aDelim = StringRegExp($sString, "{.}", 3)
$aParts = StringSplit(StringRegExpReplace($sString, "{.}", "*|*"), "*|*", 3)

For $i = 0 To UBound($aDelim) - 1
    $aParts[$i + 1] = $aDelim[$i] & $aParts[$i + 1]
Next

$aParts[0] = UBound($aDelim)
_ArrayDisplay($aParts)
Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

This should do the work :

#include <Array.au3>
$sString = ClipGet()

$aResult = StringRegExp($sString,'(?s){([^{]+)', 3)

For $i = 0 to UBound($aResult)-1
    $n = StringRegExpReplace($aResult[$i], '(?s)(^\d+).+', "$1")
    $txt = StringReplace(StringRegExpReplace($aResult[$i], '(^\d+}\s*)', ""), @crlf, " ")
   FileWrite(@DesktopDir & "\sura " & $n & ".txt" , $txt & @CRLF)
Next
Edited by mikell

Share this post


Link to post
Share on other sites

Terenz,

Another alternative...

#include <array.au3>
#include <file.au3>

local $str = fileread(@scriptdir & '\' & 'test10.txt')

local $array = stringregexp($str,'(?m)\{[^\{]+',3)      ; include curly braces
;local $array = stringregexp($str,'(?m)\} ([^\{]+)',3)  ; exclude curly braces

_FileWriteFromArray(@scriptdir & '\test010_out.txt',$array)

shellexecute(@scriptdir & '\test010_out.txt')

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

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

  • Similar Content

    • ViciousXUSMC
      By ViciousXUSMC
      So I ran into this crazy "program" that cant be uninstalled via WMI, MSIExec, etc.
      The only way to uninstall it was from Add/Remove programs manually... Or I found if you find it in the registry under HKCU and run the  uninstall string, it will also uninstall.
      However the string in the registry cant be run directly in a cmd window because of the format errors.
      It has spaces without quotations, it has invalid characters, etc, etc 
      I know things run different when executed in the registry, so maybe there is a way I can run the regsitry key just like how the system does?  If so chime in.
      Otherwise I did this a crude way using several stringregexpreplace() functions and have it working.
      The solution feels so barbaric and crude that I wanted to post it so some of you guys better than me can clean up the code, maybe offer alternative ways to do it, or reduce the number of times I process the string.
      Here is the string right out of the registry:
      c:\Program Files\Common Files\Microsoft Shared\VSTO\10.0\VSTOInstaller.exe /Uninstall file:///C:/Users/it022565/AppData/Local/Temp/OOBAXTOWordAddIn/ApplicationXtender.AXTO.Word.vsto Here is my cave man scripting to turn this into a run able string.
       
      Func _UninstallOld() For $i = 1 to 100 ;Enumerate Registry $sEnumBase = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" ;Look in HKCU for the uninstall string for the old version $sEnum = RegEnumKey($sEnumBase, $i) If @Error Then Return If $iDebug = 1 Then MsgBox(0, "", $sEnum) If StringInStr(RegRead($sEnumBase & $sEnum, "DisplayName"), "Word Addin") Then ExitLoop Next If $iDebug = 1 Then MsgBox(0, "", $sEnum) $sKey = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $sEnum $sKey2 = RegRead($sKey, "UninstallString") If $iDebug = 1 Then MsgBox(0, "Original Install Location", $sKey2) $sKey3 = StringRegExpReplace($sKey2, "(?i)(c:.*exe)", '"$1"') If $iDebug = 1 Then MsgBox(0, "", $sKey3) $sKey4 = StringRegExpReplace($sKey3, "(?i)file:///", "") If $iDebug = 1 Then MsgBox(0, "", $sKey4) $sKey5 = StringRegExpReplace($sKey4, "%20", " ") If $iDebug = 1 Then MsgBox(0, "", $sKey5) $sKey6 = StringRegExpReplace($sKey5, '(?i)((?<!")c:.*vsto)', '"$1"') If $iDebug = 1 Then MsgBox(0, "", $sKey6) RunWait(@ComSpec & ' /c ' & '"' & $sKey6 & ' /s"', "", @SW_HIDE) EndFunc Basically step by step I add quotations, strip bad characters, etc.  Kind of proud for using look behind for once
      Looking forward to what you guys come up with.
    • VIP
      By VIP
      Need help to make function better  with full infomation
      #include <Array.au3> #include <File.au3> _TEST(@ScriptFullPath) _TEST("A:") _TEST("A:\B.c") _TEST("D:\E\F\") _TEST("G:\H/../J.k/") _TEST("M:\N\k..J.k") _TEST("D:\E\F\..\G\G\I..J.K.M") Func _TEST($sFilePath) Local $sDrive = "", $sFullPathDir = "", $sDirPath = "", $sDirName = "", $sFileName = "", $sFileNameExt = "", $sExtension = "", $sExt = "" Local $aPathSplit = _PathSplitByRef($sFilePath, $sDrive, $sFullPathDir, $sDirPath, $sDirName, $sFileName, $sFileNameExt, $sExtension, $sExt) ConsoleWrite("!Path IN : " & $sFilePath & @CRLF) ; C:\Windows\System32\etc\hosts.exe ConsoleWrite("- Driver : " & $sDrive & @CRLF) ; C: ConsoleWrite("- DirPath : " & $sFullPathDir & @CRLF) ; C:\Windows\System32\etc\etc ConsoleWrite("- DirPath : " & $sDirPath & @CRLF) ; \Windows\System32\etc\ ConsoleWrite("- DirName : " & $sDirName & @CRLF) ; etc ConsoleWrite("- FileName : " & $sFileName & @CRLF) ; hosts ConsoleWrite("- FileNameExt: " & $sFileNameExt & @CRLF) ; hosts.exe ConsoleWrite("- Extension : " & $sExtension & @CRLF) ; .exe ConsoleWrite("- Ext : " & $sExt & @CRLF & @CRLF) ; exe ;~ ConsoleWrite("!Path IN : " & $aPathSplit[0] & @CRLF) ; C:\Windows\System32\etc\hosts.exe ;~ ConsoleWrite("- Driver : " & $aPathSplit[1] & @CRLF) ; C: ;~ ConsoleWrite("- DirPath : " & $aPathSplit[2] & @CRLF) ; C:\Windows\System32\etc\etc ;~ ConsoleWrite("- DirPath : " & $aPathSplit[3] & @CRLF) ; \Windows\System32\etc\ ;~ ConsoleWrite("- DirName : " & $aPathSplit[4] & @CRLF) ; etc ;~ ConsoleWrite("- FileName : " & $aPathSplit[5] & @CRLF) ; hosts ;~ ConsoleWrite("- FileNameExt: " & $aPathSplit[6] & @CRLF) ; hosts.exe ;~ ConsoleWrite("- Extension : " & $aPathSplit[7] & @CRLF) ; .exe ;~ ConsoleWrite("- Ext : " & $aPathSplit[8] & @CRLF) ; exe ;~ _ArrayDisplay($aPathSplit, "_PathSplit of " & $sFilePath) EndFunc ;==>_TEST Func _PathSplitByRef($sFilePath, ByRef $sDrive, ByRef $sFullPathDir, ByRef $sDirPath, ByRef $sDirName, ByRef $sFileName, ByRef $sFileNameExt, ByRef $sExtension, ByRef $sExt) If StringInStr($sFilePath,"..") Then $sFilePath=_PathFull($sFilePath) Local $aPartOfPath=StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH) ;~ If @error Then ReDim $aPartOfPath[9] ;~ $aPartOfPath[0] = $sFilePath ;~ EndIf $aPartOfPath[0] = $sFilePath ; C:\Windows\System32\etc\hosts.exe $sDrive = $aPartOfPath[1] ; C: $sFullPathDir = $aPartOfPath[1] & $aPartOfPath[2] ; C:\Windows\System32\etc If StringLeft($aPartOfPath[2], 1) == "/" Then $sDirPath = StringRegExpReplace($aPartOfPath[2], "\h*[\/\\]+\h*", "\/") Else $sDirPath = StringRegExpReplace($aPartOfPath[2], "\h*[\/\\]+\h*", "\\") EndIf $aPartOfPath[2] = $sFullPathDir ; C:\Windows\System32\etc $sDirName=StringReplace($sDirPath,"\","") $sDirName=StringReplace($sDirPath,"/","") $sFileName = $aPartOfPath[3] ; hosts $aPartOfPath[5] = $sFileName ; hosts $sExtension = $aPartOfPath[4] ; .exe $aPartOfPath[7] = $sExtension ; .exe $aPartOfPath[3] = $sDirPath ; \Windows\System32\etc\ $aPartOfPath[4] = $sDirName ; etc $aPartOfPath[6] = $sFileName & $sExtension ; hosts.exe $sFileNameExt = $aPartOfPath[6] ; hosts.exe $sExt = StringReplace($sExtension,".","") ; exe $aPartOfPath[8] = $sExt ; exe Return $aPartOfPath EndFunc ;==>_PathSplitByRef  
    • hawkair
      By hawkair
      Hi
      I am trying to insert line numbers in to a string
      with this script
      Func _MyInc () Static Local $i = 0 $i += 1 Return $i EndFunc Exit _InsertLines() Func _InsertLines()     $String = "A" & @CRLF & "B" & @CRLF & "C" & @CRLF & "D" $NewString =  Execute("'" & StringRegExpReplace($String,"[\r\n]*",  "' & _MyInc () & '\1" ) & "'") MsgBox (0, "", $NewString) EndFunc but I get this:
      1A23B45C67D8
      I never really could master how Execute works here and I always get some working example and make substitutions.
      But this is the closest i could get...
       
    • AutoBert
      By AutoBert
      The idea to use translation api:

      i used the script from @mikell to build this func:
      Func _Translate($sFrom, $from, $to) ;thanks to mikell (autoitscript.com) ;https://www.autoitscript.com/forum/topic/182893-prompt-me-how-to-see-the-text-in-the-translation-boxhttpstranslategooglecom/?do=findComment&comment=1313423 Local $url = "https://translate.googleapis.com/translate_a/single?client=gtx" $url &= "&sl=" & $from & "&tl=" & $to & "&dt=t&q=" & $sFrom Local $oHTTP = ObjCreate("Microsoft.XMLHTTP") $oHTTP.Open("POST", $url, False) $oHTTP.Send() Local $sData = $oHTTP.ResponseText $sData = StringRegExpReplace($sData, '.*?\["(.*?)"[^\[]*', "$1" & @CRLF) Return $sData EndFunc ;==>_Translate when i call this func with:
      $sText='AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying "runtimes" required!' MsgBox(64,'',_Translate($sText,'en','de')) nearly all is seeing here:

      only the "!" is wrong "\" but when using 'auto' instead of 'en' the result is:

      2 lines are appended. So my question is, is it possible to extend the pattern (i never worked with regex) and in best case setting @extended with the detected language?
      @Trong: as you can see yet i am returning translated text and don't use GuiCtrlSetData to assign it to a EditBox.
    • ViciousXUSMC
      By ViciousXUSMC
      I was working on something last night and decided to use StringRegExpReplace() for a config file, I never noticed that you cant just "overwrite" the file with the update so easily it required a few more pieces of code to work properly.
      Is this the simplest way (what I used) and while I searched for it and did not find it do we have or will we have a RegEx equivalent for _ReplaceStringInFile()?
      $sFile = FileRead(@ScriptDir & "\test.txt") $hFile = FileOpen(@ScriptDir & "\test.txt", 2) $sNewContent = StringRegExpReplace($sFile, "(test)", "new$1") FileWrite($hFile, $sNewContent) FileClose($hFile)