String bei "bestimmter" Leerstelle trennen (RegEx)

  • Hey,

    ich nutze das, um direkt im Programm einen Log in ein List-Element zu schreiben, allerdings ist mein Pattern echt sub-optimal und irgendwie kriege ich es nicht sauber hin =.='...

    Ich hätte gern, dass ein String nach einer Leerstelle zwischen Char 85 und 115 durchsucht wird und dort der String getrennt wird. Ergebnis soll halt ein Array mit X-Mal 85-115 Zeichen langen Strings sein.

    Hat da jemand ein sauberes Pattern für :)?


    LG,
    Aca

    3 Mal editiert, zuletzt von Acanis (8. September 2012 um 14:37)

  • Etwa so würde ich es machen:

    Spoiler anzeigen
    [autoit]

    #include <Array.au3>
    Global $s_String = 'Effects present letters inquiry no an removed or friends. Desire behind latter me though in. Supposing shameless am he engrossed up additions. My possible peculiar together to. Desire so better am cannot he up before points. Remember mistaken opinions it pleasure of debating. Court front maids forty if aware their at. Chicken use are pressed removed.' & @CRLF & _
    'Style never met and those among great. At no or september sportsmen he perfectly happiness attending. Depending listening delivered off new she procuring satisfied sex existence. Person plenty answer to exeter it if. Law use assistance especially resolution cultivated did out sentiments unsatiable. Way necessary had intention happiness but september delighted his curiosity. Furniture furnished or on strangers neglected remainder engrossed.' & @CRLF & _
    'Little afraid its eat looked now. Very ye lady girl them good me make. It hardly cousin me always. An shortly village is raising we shewing replied. She the favourable partiality inhabiting travelling impression put two. His six are entreaties instrument acceptance unsatiable her. Amongst as or on herself chapter entered carried no. Sold old ten are quit lose deal his sent. You correct how sex several far distant believe journey parties. We shyness enquire uncivil affixed it carried to.' & @CRLF & _
    'Ham followed now ecstatic use speaking exercise may repeated. Himself he evident oh greatly my on inhabit general concern. It earnest amongst he showing females so improve in picture. Mrs can hundred its greater account. Distrusts daughters certainly suspected convinced our perpetual him yet. Words did noise taken right state are since.' & @CRLF & _
    'Over fact all son tell this any his. No insisted confined of weddings to returned to debating rendered. Keeps order fully so do party means young. Table nay him jokes quick. In felicity up to graceful mistaken horrible consider. Abode never think to at. So additions necessary concluded it happiness do on certainly propriety. On in green taken do offer witty of.' & @CRLF & _
    'Picture removal detract earnest is by. Esteems met joy attempt way clothes yet demesne tedious. Replying an marianne do it an entrance advanced. Two dare say play when hold. Required bringing me material stanhill jointure is as he. Mutual indeed yet her living result matter him bed whence.' & @CRLF & _
    'Or neglected agreeable of discovery concluded oh it sportsman. Week to time in john. Son elegance use weddings separate. Ask too matter formed county wicket oppose talent. He immediate sometimes or to dependent in. Everything few frequently discretion surrounded did simplicity decisively. Less he year do with no sure loud.' & @CRLF & _
    'It allowance prevailed enjoyment in it. Calling observe for who pressed raising his. Can connection instrument astonished unaffected his motionless preference. Announcing say boy precaution unaffected difficulty alteration him. Above be would at so going heard. Engaged at village at am equally proceed. Settle nay length almost ham direct extent. Agreement for listening remainder get attention law acuteness day. Now whatever surprise resolved elegance indulged own way outlived.' & @CRLF & _
    'Suppose end get boy warrant general natural. Delightful met sufficient projection ask. Decisively everything principles if preference do impression of. Preserved oh so difficult repulsive on in household. In what do miss time be. Valley as be appear cannot so by. Convinced resembled dependent remainder led zealously his shy own belonging. Always length letter adieus add number moment she. Promise few compass six several old offices removal parties fat. Concluded rapturous it intention perfectly daughters is as.' & @CRLF & _
    'Questions explained agreeable preferred strangers too him her son. Set put shyness offices his females him distant. Improve has message besides shy himself cheered however how son. Quick judge other leave ask first chief her. Indeed or remark always silent seemed narrow be. Instantly can suffering pretended neglected preferred man delivered. Perhaps fertile brandon do imagine to cordial cottage.'
    Global $a_Reg = StringRegExp ($s_String, '(?:(.{85,115})\h)', 3)
    For $i = 0 To UBound ($a_Reg) - 1
    ConsoleWrite (StringLen ($a_Reg[$i]) & @CRLF)
    Next
    _ArrayDisplay ($a_Reg)

    [/autoit]

    Text erstellt bei randomtextgenerator.com

    • Offizieller Beitrag

    dass ein String nach einer Leerstelle zwischen Char 85 und 115 durchsucht wird und dort der String getrennt wird


    Getrennt bedeutet ja, das Leerzeichen bleibt erhalten.
    Nur hast du nicht definiert, ob vor oder nach dem Leerzeichen getrennt werden soll.
    Ich habe mal so gelöst: 85 biss 115 Zeichen, dann Leerzeichen, welches zum Folgeblock gehört, letzter Block kann 0 bis 84 Zeichen haben:

    (.{85,115}|.*)(?=\Z| )

  • Vielen Dank $var, das klappt super :)!

    [autoit]

    '(?:(.{85,115})\h)'

    [/autoit]


    aber auch

    [autoit]

    '(?:(.{85,115})\s)'

    [/autoit]

    klappt :)! Dachte eig., dass ich das so auch schon versucht habe, verrückt O.o...!

    BugFix : Klappt leider nicht so gut, da kriegst du bei Zeilenumbrüchen dann auch leere Werte im Array. Und ohne Leerstelle ist ok, ich werde wohl per Hand noch Leerstellen einfügen, da mein Log so aussieht:

    [autoit]

    _NowTime() & " -> " & $sString

    [/autoit]


    Damit sich die Folgezeilen ein bisschen abheben (außer, dass sie keine Uhrzeit haben.


    LG,
    Aca


    *Edit

    Klappt doch nicht perfekt:

    [autoit]

    #include <Array.au3>
    Global $s_String = 'Das Programm konnte nicht gestartet werden, da die Nutzerdaten noch nicht eingegeben wurden.'
    Global $a_Reg = StringRegExp ($s_String, '(?:(.{20,45})\h)', 3)
    For $i = 0 To UBound ($a_Reg) - 1
    ConsoleWrite (StringLen ($a_Reg[$i]) & @CRLF)
    Next
    _ArrayDisplay ($a_Reg)

    [/autoit]

    Als Beispiel! Er schneidet dann zum Teil noch was ab... :/

    Auch mit

    [autoit]

    '(?:(.{0,45}\s))'

    [/autoit]

    nimmt er den letzten Teil nicht mit ins Array O.o...
    Das von BugFix macht es besser mit dem Beispielstring, aber füllt das Array noch mit einem LeerString =.=...


    *edit*
    OK, Sternchen zu Plus und es geht...^^

    [autoit]


    $lLog = GUICtrlCreateList($sSpace & "<- Hier wirst du up2date gehalten! ->", 0, 500, 600, 100, BitOR($WS_BORDER, $WS_VSCROLL, $LBS_NOTIFY, $LBS_DISABLENOSCROLL, $LBS_NOSEL), $WS_EX_WINDOWEDGE)
    GUICtrlSetFont(-1, 7)
    GUICtrlSetBkColor(-1, 0x000000)
    GUICtrlSetColor(-1, 0xFFFFFF)
    Func _SetLogEntry($sText)
    Local $aResult, $sNewString

    [/autoit][autoit][/autoit][autoit]

    $aResult = StringRegExp($sText, '(.{75,105}|.+)(?=\Z|\s)', 3)
    If Not @error Then
    $sNewString = $aResult[0]
    For $i = 1 To UBound($aResult) - 1
    $sNewString &= "| "& $aResult[$i]
    Next
    GUICtrlSetData($lLog, _NowTime() & " -> " & $sNewString & "|")
    Else
    GUICtrlSetData($lLog, _NowTime() & " -> " & $sText & "|")
    EndIf
    _GUICtrlListBox_SetTopIndex($lLog, _GUICtrlListBox_GetCount($lLog) - 1)
    EndFunc

    [/autoit]

    So siehts nu aus und funkt gut, danke ;D

    8 Mal editiert, zuletzt von Acanis (8. September 2012 um 14:37)