Jump to content
Sign in to follow this  
AutID

String functions / StringInStr

Recommended Posts

AutID

Hello,

hmm i don't really know how to explain this :/

I have a string like this

$sText = "here as"
$sString = "some text for me" & @LF & _
"some" & @LF & _
"and some other text here as well line test" & @LF & _ ;here
"herealso" & @LF & _
"Sometesthere" & @LF & _
"artart43" & @LF & _
"aastreter " & @LF & _
"[dfg"
I want to get the text from "other" until "line"

I tried StringTrim functions but they do exactly what i thought they did.

Local $stringpos = StringInStr($sString, $sText)
If $stringpos Then
$sString = 0 ;StringRegExpReplace pattern or something else which i don't know...
ConsoleWrite($sString & @CRLF)
Else
ConsoleWrite("error" & @CRLF)
EndIf
Edited by AutID

Share this post


Link to post
Share on other sites
Melba23

AutID,

I would get that text like this:

#include <Constants.au3>

$sString = "some text for me" & @LF & _
"some" & @LF & _
"and some other text here as well line test" & @LF & _ ;here
"herealso" & @LF & _
"Sometesthere" & @LF & _
"artart43" & @LF & _
"aastreter " & @LF & _
"[dfg"

$sExtract = StringRegExpReplace($sString, "(?s).*other\s(.*)\sline.*", "$1")

MsgBox($MB_SYSTEMMODAL, "Extract", $sExtract)
SRER decode:

(?s)      - Treat newline strings (@CR/LF/CRLF) as any other
.*other\s - Look for "other{space}" and then
(.*)      - capture all characters up to
\sline.*  - "(space)line"

$1        - Replace everything with the capture group
I hope that helps. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Malkey

Here is a non-regular expression method.

Local $sString = "some text for me" & @LF & _
        "some" & @LF & _
        "and some other text here as well line test" & @LF & _ ;here
        "herealso" & @LF & _
        "Sometesthere" & @LF & _
        "artart43" & @LF & _
        "aastreter " & @LF & _
        "[dfg"

Local $sStartWord = "other"
Local $sLastWord = "line"

Local $iStart = StringInStr($sString, $sStartWord) + StringLen($sStartWord)
Local $sExtract = StringStripWS(StringMid($sString, $iStart, StringInStr($sString, $sLastWord) - $iStart), 3)

MsgBox(0, "Extract", $sExtract)

Share this post


Link to post
Share on other sites
mikell

An other one

#include <String.au3>

Local $sString = "some text for me" & @LF & _
        "some" & @LF & _
        "and some other text here as well line test" & @LF & _ ;here
        "herealso" & @LF & _
        "Sometesthere" & @LF & _
        "artart43" & @LF & _
        "aastreter " & @LF & _
        "[dfg"

Local $sExtract = _StringBetween($sString, "other", "line")
MsgBox(0, "Extract", StringStripWS($sExtract[0], 3)))
Edited by mikell

Share this post


Link to post
Share on other sites
AutID

Edit: Of course thank you all guys for the help and sorry for forgetting it :/

I came up with this regular exprasion but it isn't excactly what i wanted to do.

StringRegExpReplace($sString, "(?s).*\n(.*)\s.*", "$1")
I want to look for a newline and then capture till a {space}. Shouldn't it be like this? Edited by AutID

Share this post


Link to post
Share on other sites
kylomas

AutID,

"I want to look for a newline and then capture till a {space}."

This will do that...

 

#include <array.au3>

$sText = "here as"
$sString = "some text for me" & @LF & _
"some" & @LF & _
"and some other text here as well line test" & @LF & _ ;here
"herealso" & @LF & _
"Sometesthere" & @LF & _
"artart43" & @LF & _
"aastreter " & @LF & _
"[dfg"

local $array = stringregexp($sString,'(?m)\R(.*?) ',3)

_arraydisplay($array)

What if there is a n, followed by n-chrs, followed by a n (no intervening space)?
 

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
AutID

AutID,

"I want to look for a newline and then capture till a {space}."

This will do that...

#include <array.au3>

$sText = "here as"
$sString = "some text for me" & @LF & _
"some" & @LF & _
"and some other text here as well line test" & @LF & _ ;here
"herealso" & @LF & _
"Sometesthere" & @LF & _
"artart43" & @LF & _
"aastreter " & @LF & _
"[dfg"

local $array = stringregexp($sString,'(?m)\R(.*?) ',3)

_arraydisplay($array)
What if there is a n, followed by n-chrs, followed by a n (no intervening space)?

 

kylomas

You missed to read the 1st post :D

I am searching for a keyword in the text and then get the line from the beggining until the first space

Share this post


Link to post
Share on other sites
kylomas

Actually I read your first post and all of the WORKING solutions.  It seemed that your requirements changed.  Now I have no idea what you want.

Good Luck,

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
AutID

Actually I read your first post and all of the WORKING solutions.  It seemed that your requirements changed.  Now I have no idea what you want.

 

Good Luck,

 

kylomas

Ok

Guys got it working. Thank you everyone. This is what i came up with:

Local Const $stext = "has left"

$diff = "some text for me" & @LF & _
        "some" & @LF & _
        "and some other Palclca (test) has left the text here as well line test" & @LF & _ ;here
        "herealso" & @LF & _
        "Sometesthere" & @LF & _
        "artart43" & @LF & _
        "aastreter " & @LF & _
        "[dfg"




Local $stringpos = StringInStr($diff, $stext)
Local $try = 0
If $stringpos Then
    Local $res
    $diff = StringRegExp($diff, "(?:([^\v]+)(?:\v+|$))", 3)
    If IsArray($diff) Then
        For $i = 0 To UBound($diff) - 1
            If StringInStr($diff[$i], $stext) Then
                $res = $diff[$i]
                $res = StringRegExpReplace($res, "\s.*", "$1")
                ConsoleWrite($res & @CRLF)
            EndIf
        Next
    Else
        ConsoleWrite("Array doesn't exists" & @CRLF)
    EndIf
Else
    ConsoleWrite("String doesn't exists" & @CRLF)
EndIf
Edited by AutID

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  

×