Jump to content

StringBetween (SRER)


Deye
 Share

Go to solution Solved by mikell,

Recommended Posts

Been trying this for a while now

@Getting lines between 3. To protect .*\n <(.*)>|8. Do not delete

Not sure what else to try ..

$str = 'This list is non-exhaustive - the Moderating team reserve the right to close any thread that they fe' _
         & 'el is contrary to the ethos of the forum.' & @LF & @CRLF
$str &= '2. Do not post material that could be considered pornographic, violent or explicit - or express pers' _
         & 'onal opinions that would not be acceptable in a civilized society. Do not post any copyrighted mater' _
         & 'ial unless the copyright is owned by you or by this site.' & @LF & @CRLF
$str &= '3. To protect this community, any files posted by you are subject to checks to ensure that they do n' _
         & 'ot contain malware. This includes, but is not limited to, decompilation and reverse engineering.' & @LF & @CRLF
$str &= '4. Do not flame or insult other members - and just report the thread to a Moderator (see below) if y' _
         & 'ou are so attacked.' & @LF & @CRLF
$str &= '5. Do not PM other users asking for support - that is why the forum exists, so post there instead.' & @LF & @CRLF
$str &= '6. Do not create multiple accounts - if you inadvertently created multiple accounts then contact a M' _
         & 'oderator to close the unwanted ones.' & @LF & @CRLF
$str &= '7. Do not repost the same question if the previous thread has been locked - particularly if you mere' _
         & 'ly reword the question to get around one of the prohibitions listed above.' & @LF & @CRLF
$str &= '8. Do not delete your posts, nor completely remove their content, if doing so will interrupt the flo' _
         & 'w of the thread.' & @LF & @CRLF
$str &= '9. Do not post in a thread while the Moderating team are actively trying to determine whether it is ' _
         & 'legal.' & @LF & @CRLF
$str &= 'The Moderation team will do their best to act in fair and reasonable manner. Sanctions will only be ' _
         & 'applied as a last resort and any action taken will be explained in the relevant thread.' & @LF & @CRLF
$str &= 'If moderation action is taken, you will need to acknowledge this through a dialog or you will be una' _
         & 'ble to post further in the forum. Please note that this dialog is not an agreement that the warning ' _
         & 'was justified - it is only there so that members are aware that moderation action has been taken and' _
         & ' that they may have certain restrictions applied to their account.' & @LF & @CRLF
$str &= 'If you feel that you have been unfairly moderated then contact the Moderator concerned - using a PM ' _
         & 'or the "Report" button is preferable to opening a new thread (although new members may have to do th' _
         & 'is). But do be aware that the Moderation team has the final word - the rules are set out by the site' _
         & ' owner and you are only welcome here if you respect his wishes.'

$sOutput =  StringRegExpReplace($str, "(.)(\Q3. To protect\E.*\n\Q8. Do not delete\E)", "$2")

MsgBox("","", $sOutput)

Thanks

Edited by Deye
Link to comment
Share on other sites

Or if you want to stick with StringRegExpReplace, this is one way that'll work:

$sOutput =  StringRegExpReplace($str, "(?sm).*(^3[.].*^8[.].*?$).*", "\1")

It's not pretty, but it will work.

 

Link to comment
Share on other sites

I need to start with any unique string in a line skipping that line going to next unique string in some other line delimiting it and get what's between those two string mentioned lines

Not there yet:

MsgBox("", "", _between("protect", "If moderation action"))

Func _between($s1, $s2)
    Return StringStripWS(StringRegExpReplace($str, ".*\n\R(.*\Q" & $s1 & "\E.*)|(.*\Q" & $s2 & "\E.*\W\R.*)", ""), 3)
EndFunc   ;==>_between

 

Edited by Deye
Link to comment
Share on other sites

Or create your own user-defined function (or should I say use something like my example below) :muttley:.   Everything doesn't have to be a regular expression.

Spoiler
#include <Constants.au3>

$str = 'This list is non-exhaustive - the Moderating team reserve the right to close any thread that they fe' _
         & 'el is contrary to the ethos of the forum.' & @LF & @CRLF
$str &= '2. Do not post material that could be considered pornographic, violent or explicit - or express pers' _
         & 'onal opinions that would not be acceptable in a civilized society. Do not post any copyrighted mater' _
         & 'ial unless the copyright is owned by you or by this site.' & @LF & @CRLF
$str &= '3. To protect this community, any files posted by you are subject to checks to ensure that they do n' _
         & 'ot contain malware. This includes, but is not limited to, decompilation and reverse engineering.' & @LF & @CRLF
$str &= '4. Do not flame or insult other members - and just report the thread to a Moderator (see below) if y' _
         & 'ou are so attacked.' & @LF & @CRLF
$str &= '5. Do not PM other users asking for support - that is why the forum exists, so post there instead.' & @LF & @CRLF
$str &= '6. Do not create multiple accounts - if you inadvertently created multiple accounts then contact a M' _
         & 'oderator to close the unwanted ones.' & @LF & @CRLF
$str &= '7. Do not repost the same question if the previous thread has been locked - particularly if you mere' _
         & 'ly reword the question to get around one of the prohibitions listed above.' & @LF & @CRLF
$str &= '8. Do not delete your posts, nor completely remove their content, if doing so will interrupt the flo' _
         & 'w of the thread.' & @LF & @CRLF
$str &= '9. Do not post in a thread while the Moderating team are actively trying to determine whether it is ' _
         & 'legal.' & @LF & @CRLF
$str &= 'The Moderation team will do their best to act in fair and reasonable manner. Sanctions will only be ' _
         & 'applied as a last resort and any action taken will be explained in the relevant thread.' & @LF & @CRLF
$str &= 'If moderation action is taken, you will need to acknowledge this through a dialog or you will be una' _
         & 'ble to post further in the forum. Please note that this dialog is not an agreement that the warning ' _
         & 'was justified - it is only there so that members are aware that moderation action has been taken and' _
         & ' that they may have certain restrictions applied to their account.' & @LF & @CRLF
$str &= 'If you feel that you have been unfairly moderated then contact the Moderator concerned - using a PM ' _
         & 'or the "Report" button is preferable to opening a new thread (although new members may have to do th' _
         & 'is). But do be aware that the Moderation team has the final word - the rules are set out by the site' _
         & ' owner and you are only welcome here if you respect his wishes.'




MsgBox("","", extract_lines("protect", "posts"))

Func extract_lines($sStart, $sEnd)
    Local $aLinesIn[0]
    Local $sOutput = ""
    Local $iCurrentLine = 0

    ;Split lines into an arry
    $aLinesIn = StringSplit($str, @CRLF, $STR_ENTIRESPLIT)

    ;Spin thru lines in
    For $i = 0 To UBound($aLinesIn) - 1
        ;If starting string found
        If StringInStr($aLinesIn[$i], $sStart) Then
            ;Start outputting lines until end of array or ending string found
            $iCurrentLine = $i
            While $iCurrentLine < UBound($aLinesIn)
                $sOutput &= $aLinesIn[$iCurrentLine]
                If StringInStr($aLinesIn[$iCurrentLine], $sEnd) Then ExitLoop 2
                $iCurrentLine += 1
            WEnd
        EndIf
    Next

    Return $sOutput
EndFunc

 

 

Edited by TheXman
Link to comment
Share on other sites

Link to comment
Share on other sites

  • Solution
9 hours ago, TheXman said:

Everything doesn't have to be a regular expression.

Yes... though RE can be useful sometimes :rolleyes:

$str = 'This list is non-exhaustive - the Moderating team reserve the right to close any thread that they fe' _
         & 'el is contrary to the ethos of the forum.' & @LF & @CRLF
$str &= '2. Do not post material that could be considered pornographic, violent or explicit - or express pers' _
         & 'onal opinions that would not be acceptable in a civilized society. Do not post any copyrighted mater' _
         & 'ial unless the copyright is owned by you or by this site.' & @LF & @CRLF
$str &= '3. To protect this community, any files posted by you are subject to checks to ensure that they do n' _
         & 'ot contain malware. This includes, but is not limited to, decompilation and reverse engineering.' & @LF & @CRLF
$str &= '4. Do not flame or insult other members - and just report the thread to a Moderator (see below) if y' _
         & 'ou are so attacked.' & @LF & @CRLF
$str &= '5. Do not PM other users asking for support - that is why the forum exists, so post there instead.' & @LF & @CRLF
$str &= '6. Do not create multiple accounts - if you inadvertently created multiple accounts then contact a M' _
         & 'oderator to close the unwanted ones.' & @LF & @CRLF
$str &= '7. Do not repost the same question if the previous thread has been locked - particularly if you mere' _
         & 'ly reword the question to get around one of the prohibitions listed above.' & @LF & @CRLF
$str &= '8. Do not delete your posts, nor completely remove their content, if doing so will interrupt the flo' _
         & 'w of the thread.' & @LF & @CRLF
$str &= '9. Do not post in a thread while the Moderating team are actively trying to determine whether it is ' _
         & 'legal.' & @LF & @CRLF
$str &= 'The Moderation team will do their best to act in fair and reasonable manner. Sanctions will only be ' _
         & 'applied as a last resort and any action taken will be explained in the relevant thread.' & @LF & @CRLF
$str &= 'If moderation action is taken, you will need to acknowledge this through a dialog or you will be una' _
         & 'ble to post further in the forum. Please note that this dialog is not an agreement that the warning ' _
         & 'was justified - it is only there so that members are aware that moderation action has been taken and' _
         & ' that they may have certain restrictions applied to their account.' & @LF & @CRLF
$str &= 'If you feel that you have been unfairly moderated then contact the Moderator concerned - using a PM ' _
         & 'or the "Report" button is preferable to opening a new thread (although new members may have to do th' _
         & 'is). But do be aware that the Moderation team has the final word - the rules are set out by the site' _
         & ' owner and you are only welcome here if you respect his wishes.'



;MsgBox(0,"", extract_lines($str, "protect", "posts"))
;MsgBox(0,"", extract_lines($str, "non-exhaustive", "ethos"))
;MsgBox(0,"", extract_lines($str, "protect", ""))
;MsgBox(0,"", extract_lines($str, "", "posts"))
MsgBox(0,"", extract_lines($str, "protect", "?"))

Func extract_lines($txt, $sStart, $sEnd)
    Return StringRegExpReplace($txt, '(?s)(?:^|.*\R)(\N*?' & $sStart & '.*' & $sEnd & '\N*).*', "$1")
EndFunc

 

Edited by mikell
Tiny modif to make the srer more versatile :)
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...