Jump to content

Recommended Posts

Posted (edited)

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
  • Moderators
Posted

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

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:

  Reveal hidden contents

 

Posted

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)
Posted (edited)

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
Posted

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

Posted

  On 11/14/2013 at 3:17 AM, kylomas said:

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

Posted

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

Posted (edited)

  On 11/14/2013 at 4:04 AM, kylomas said:

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...