Jump to content

Finding a text between 2 markers


 Share

Recommended Posts

Hi,

So I'm trying to find a given text in a .txt file that has 200 to 500 lines and the text that I want is allways between # 010 # and # 101 #

For example:

Lorem ipsum dolor sit amet, consectetur adipisicing elit,

sed do eiusmod tempor incididunt ut labore et dolore

magna aliqua. Ut enim ad minim veniam, quis nostrud

exercitation ullamco laboris nisi ut aliquip ex ea commodo

# 010 #consequat. Duis aute irure dolor in reprehenderit

in voluptate velit esse cillum dolore eu fugiat nulla# 101 #

pariatur. Excepteur sint occaecat cupidatat non proident,

sunt in culpa qui officia deserunt mollit anim id est laborum

What I want is to put the text in bold in a variable.

Any help woul be appreciated.

thx in advance

Edited by Dryden

"Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life." - Terry Pratchett.

Link to comment
Share on other sites

I couldn't find the post that I first found the following snippet wrote by someone else and I take no credit for it, but it works great for what your after

To use it $str = "# 010 #Your String of course# 101 #", $start = '# 010 #', $end = '# 101 #'

; A Modified String < Between > Search ------------------------------------------------------
Global $pos
Func stringbetween($str, $start, $end)
$pos = StringInStr($str, $start)
If Not @error Then
$str = StringTrimLeft($str, $pos + StringLen($start) - 1)
$pos = StringInStr($str, $end)
If Not @error Then
$str = StringTrimRight($str, StringLen($str) - $pos + 1)
Return $str
EndIf
EndIf
EndFunc ;==>stringbetween
Edited by Thamiel
Link to comment
Share on other sites

$source = "magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo# 010 #consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla# 101 # pariatur. Excepteur sint occaecat cupidatat non proident,"


$StartMarkerText = "# 010 #"
$EndMarkerText = "# 101 #"

$start = StringInStr($source,$StartMarkerText) + StringLen($StartMarkerText)
$end = StringInStr($source,$EndMarkerText)
$length = $end - $start

$newvalue = StringMid($source,$start,$length)

ConsoleWrite($newvalue)

Edit: I posted this before seeing the post above - that function is probably a better route, especially if you need to do that task with a lot of different starting and ending markers.

Edited by Deltron0
Link to comment
Share on other sites

Try this:

$sText = _
"Lorem ipsum dolor sit amet, consectetur adipisicing elit," & @CRLF & _
"sed do eiusmod tempor incididunt ut labore et dolore" & @CRLF & _
"magna aliqua. Ut enim ad minim veniam, quis nostrud" & @CRLF & _
"exercitation ullamco laboris nisi ut aliquip ex ea commodo" & @CRLF & _
"# 010 #consequat. Duis aute irure dolor in reprehenderit" & @CRLF & _
"in voluptate velit esse cillum dolore eu fugiat nulla# 101 #" & @CRLF & _
"pariatur. Excepteur sint occaecat cupidatat non proident," & @CRLF & _
"sunt in culpa qui officia deserunt mollit anim id est laborum"

$sCaptured = StringRegExpReplace($sText, "(?s)(.*#s*d+s*#)(.*)(#s*d+s*#.*)", "$2")
MsgBox(0, "Test", $sCaptured)

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

  • Moderators

Dryden,

And another: :D

$sSource = "magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip " & _
           "ex ea commodo# 010 #consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse " & _
           "cillum dolore eu fugiat nulla# 101 # pariatur. Excepteur sint occaecat cupidatat non proident,"

$sExtract = StringRegExpReplace($sSource, ".*010s#(.*)#s101.*", "$1")

ConsoleWrite($sExtract & @CRLF)

M23

Edited by Melba23
Forum stripped backslashes

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

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...