Jump to content

Recommended Posts

Posted

Hello, people. I have been searching the forums for an example of how to do this, but I just don't know how to word it.

I have found examples of how to search a text file for how many occurences of a word appears, but how do I do that and trim around it in certain directions? Gah. It's so confusing trying to explain.

Let's say I have a text file that saids this : 

dasdsad4|62530 No.
342344|62530 Teehee.
fsdfsd4|62530 I don't care.
gdfgd4|62530 So much pie!
23234|62530 Okay
fsdfs4|62530 Please give me pie.
adasd4|62530 Just watch
23234|62530 Autoit Rocks.
asdasd4|62530 heh
23234|62530 LOL
 
How would I search for each occurrence of "4|62530" and delete all text to the left of it and "4|62530" itself to leave me with just a word like " Please give me pie." 
 
and consolewrite each?
 
I'm not asking to be spoonfed, really, I wish I could do this on my own. But I really just don't know where to start, and I'm low on time right now :sweating:
  • Moderators
Posted (edited)

Auio42TheWin,

One way might be like this: :)

#include <StringConstants.au3>

$sString = "dasdsad4|62530 No." & @CRLF & _
    "342344|62530 Teehee." & @CRLF & _
    "fsdfsd4|62530 I don't care." & @CRLF & _
    "gdfgd4|62530 So much pie!" & @CRLF & _
    "23234|62530 Okay" & @CRLF & _
    "fsdfs4|62530 Please give me pie." & @CRLF & _
    "adasd4|62530 Just watch" & @CRLF & _
    "23234|62530 Autoit Rocks." & @CRLF & _
    "asdasd4|62530 heh" & @CRLF & _
    "23234|62530 LOL"

; Convert to an array
$aLines = StringSplit($sString, @CRLF, $STR_ENTIRESPLIT)
; And loop through each line
For $i = 1 To $aLines[0]
    ; Extract line
    $sLine = $aLines[$i]
    ; Does it contain the required string
    If StringInStr($sLine, "4|62530") Then
        ; If so, then extract the text
        ConsoleWrite(StringRegExpReplace($sLine, ".*4\|62530\s(.*)", "$1") & @CRLF)
    EndIf
Next
Now we wait for the real RegEx gurus to do it on one pass. :D

M23

Edited by Melba23
Typo

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

#include<file.au3>

Global $aFile
_FileReadToArray("test.txt" , $aFile)

for $i = 1 to $aFile[0]
    If stringinstr($aFile[$i] , "4|62530") Then consolewrite(StringRegExpReplace($aFile[$i] , ".*?4\|62530" , "") & @CRLF)
next

damn im slow, at least i was close to the reasonable suggestion..

Edited by boththose

  Reveal hidden contents

Posted (edited)

  On 3/2/2015 at 6:21 PM, Melba23 said:

Auio42TheWin,

One way might be like this: :)

#include <StringConstants.au3>

$sString = "dasdsad4|62530 No." & @CRLF & _
    "342344|62530 Teehee." & @CRLF & _
    "fsdfsd4|62530 I don't care." & @CRLF & _
    "gdfgd4|62530 So much pie!" & @CRLF & _
    "23234|62530 Okay" & @CRLF & _
    "fsdfs4|62530 Please give me pie." & @CRLF & _
    "adasd4|62530 Just watch" & @CRLF & _
    "23234|62530 Autoit Rocks." & @CRLF & _
    "asdasd4|62530 heh" & @CRLF & _
    "23234|62530 LOL"

; Convert to an array
$aLines = StringSplit($sString, @CRLF, $STR_ENTIRESPLIT)
; And loop through each line
For $i = 1 To $aLines[0]
    ; Extract line
    $sLine = $aLines[$i]
    ; Does it contain the required string
    If StringInStr($sLine, "4|62530") Then
        ; If so, then extract the text
        ConsoleWrite(StringRegExpReplace($sLine, ".*4\|62530\s(.*)", "$1") & @CRLF)
    EndIf
Next
Now we wait for the real RegEx gurus to do it on one pass. :D

M23

 

what if all the text was like all on one line? (imagine what I'm about to show you not being word-wrapped) like on one line

in a text document

http://www.example.com/Something/?test=31117267;http://www.example.com/Something/?test=20642008;http://www.example.com/Something/?test=77800124;http://www.example.com/Something/?test=9255011;http://www.example.com/Something/?test=119509981;http://www.example.com/Something/?test=119509901

 and all I wanted to get was the numbers following "?test" and print each one individually? 

 

I'm sorry if I'm confusing you, I don't think I'm very good at wording this stuff. 

Edited by Auio42TheWin
  • Moderators
Posted

Auio42TheWin,

Just post a file - those links do not work. :)

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

  On 3/2/2015 at 6:39 PM, Melba23 said:

Auio42TheWin,

Just post a file - those links do not work. :)

M23

oh they weren't real links, haha. they were just examples. sorry if i confused you.

  • Moderators
Posted

Auio42TheWin,

It is really easy to do that with a simple RegEx. But how about explaining what you are actually trying to do - we do not appreciate producing umpteen solutions to random questions. ;)

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

#include <Array.au3>

$sString="http://www.example.com/Something/?test=31117267;http://www.example.com/Something/?test=20642008;http://www.example.com/Something/?test=77800124;http://www.example.com/Something/?test=9255011;http://www.example.com/Something/?test=119509981;http://www.example.com/Something/?test=119509901"

$aMatch = StringRegExp($sString , "test=(.*?);|test=(.*?)\z" , 3)

for $i = 0 to ubound($aMatch) - 1
    If $aMatch[$i] <> "" Then consolewrite($aMatch[$i] & @CRLF)
next

the regex is dirty cause i am teh suck at them.

  Reveal hidden contents

Posted (edited)

  On 3/2/2015 at 6:47 PM, Melba23 said:

Auio42TheWin,

It is really easy to do that with a simple RegEx. But how about explaining what you are actually trying to do - we do not appreciate producing umpteen solutions to random questions. ;)

M23

 

My dad saves a collection of links to a text file, and just copies and pastes them on one line and separates them using ";". (no idea why)

All he needs is the numbers. He does this on a separate computer, and he does this occasionally, so that's how I know. 

I want to figure out how to get the numbers behind each link separately.

So I was thinking about it, and I was all like "Dad, I think I could figure out how to do this" since I know of AutoIt.  

I've been trying to find a solution.

This would help him, and me as I could use this in the future if I came across a problem like this again.

Edited by Auio42TheWin
  • Moderators
Posted

Auio42TheWin,

Then why all the guff at the beginning about "pie"? :huh:

We much prefer that you give us the real information straight away. ;)

This should work: :)

#include <Array.au3>

$sString = "http://www.example.com/Something/?test=31117267;http://www.example.com/Something/?test=20642008;http://www.example.com/Something/?test=77800124;http://www.example.com/Something/?test=9255011;http://www.example.com/Something/?test=119509981;http://www.example.com/Something/?test=119509901"

$aRet = StringRegExp($sString, "(\d+)", 3)

_ArrayDisplay($aRet, "", Default, 8)
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 (edited)

and you could always run a script on his box so that when he copies the url,  only the number remains to be pasted

while 1
If stringinstr(clipget() , "?test=") Then clipput(StringRegExp(clipget() , "test=(\d+)" , 3)[0])
sleep(30)
wend
Edited by boththose

  Reveal hidden contents

Posted

  On 3/2/2015 at 7:14 PM, Melba23 said:

Auio42TheWin,

Then why all the guff at the beginning about "pie"? :huh:

We much prefer that you give us the real information straight away. ;)

This should work: :)

#include <Array.au3>

$sString = "http://www.example.com/Something/?test=31117267;http://www.example.com/Something/?test=20642008;http://www.example.com/Something/?test=77800124;http://www.example.com/Something/?test=9255011;http://www.example.com/Something/?test=119509981;http://www.example.com/Something/?test=119509901"

$aRet = StringRegExp($sString, "(\d+)", 3)

_ArrayDisplay($aRet, "", Default, 8)
M23

 

Sorry, the only reason I gave the example with "pie" is because I couldn't think of a better way to explain it. I'm stupid

I thought I was making it easier.  :sweating:

Thanks!

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