Jump to content

Questions about extracting specified texts


Recommended Posts

Hello AutoIt Scriptwriters! 👋❤️

I have some questions about text extracting from file then save extracted texts to new text file

Q1) How to extract texts after ":" delimiter in text file? For example:

New1:Old4

New28:Old3

New21:Old2

And i want to output be this:

Old4

Old3

Old2

=================================

Q2) How to extract Proxies with their Ports in each line of text file? For example:

helloHelloBye192.168.1.1:123418292293hdhfhdbdabgsgs

And i want to output be this;

192.168.1.1:12341

The largest and valid Port number in the world is 65535

=================================

Q3) How to extract duplicate texts with case sensitive after ":" delimiter? For example:

Bugs:Hello

Bugs:Hello

Bugs:hello

And i want to output be this;

Hello

Edited by Colduction
Link to comment
Share on other sites

Look at StringSplit in the help file for questions 1 and 3, and for 3 look at _ArrayUnique after the stringsplit.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

#include <String.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>

$string = "helloHelloBye192.168.1.1:123418292293hdhfhdbdabgsgs"

$arraySplitString = StringSplit(StringRegExpReplace($string, '([^0-9.:])', ''), ':')

$result = $arraySplitString[1] & ":" & StringLeft($arraySplitString[2], 5)

MsgBox(0,"", $result)


This takes care of #2

Link to comment
Share on other sites

On 3/22/2019 at 9:02 AM, iAmNewbe said:
#include <String.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>

$string = "helloHelloBye192.168.1.1:123418292293hdhfhdbdabgsgs"

$arraySplitString = StringSplit(StringRegExpReplace($string, '([^0-9.:])', ''), ':')

$result = $arraySplitString[1] & ":" & StringLeft($arraySplitString[2], 5)

MsgBox(0,"", $result)


This takes care of #2

Thanks for helping me👍❤️

Link to comment
Share on other sites

16 hours ago, FrancescoDiMuro said:

Show us then.

Maximum port is and will always be 65535, but how do you know, in those five numbers after the IP address, where the port number finishes?

 

17 hours ago, Colduction said:

I gave hard example to get and detect maximum port of ip address

 

Link to comment
Share on other sites

Here are some example answers to the three questions of post #1.

ConsoleWrite(" ---- Ans 1 ------" & @CRLF)
$sStr1 = "New1:Old4" & @CRLF & "New28:Old3" & @CRLF & "New21:Old2" ; or,   $sStr1 = FileRead($sFileName)
$sStr1New = StringRegExpReplace($sStr1, '(.+:)', '')
ConsoleWrite($sStr1New & @CRLF)
; or

ConsoleWrite(" ---- Ans 1a ------" & @CRLF)
$sStr1NewA = ""
StringReplace($sStr1, ":", "")
$iCount = @extended
For $i = 1 To $iCount
    $sStr1NewA &= StringMid($sStr1, StringInStr($sStr1, ":", 0, $i) + 1, StringInStr($sStr1, @CR, 0, $i) - StringInStr($sStr1, ":", 0, $i))
Next
ConsoleWrite($sStr1NewA & @CRLF)

;#cs
; -------------------- Write to file -----------------
Local $sFileName = "TestFile1.txt"
If FileExists($sFileName) Then FileDelete($sFileName)
FileWrite($sFileName, StringRegExpReplace($sStr1, '(.+:)', ''))
Do
Until FileExists($sFileName)
ShellExecute($sFileName)
;#ce

ConsoleWrite(" ---- Ans 2a ------" & @CRLF)
$sStr2a = "helloHelloBye192.168.1.1:023418292293hdhfhdbdabgsgs"
$REPattern = '^\D+([\d.]+):(\d{0,6}).*$'
$REReplacement = '"\1:"&(\2<=65535?"\2":(StringTrimRight("\2",1)<=65535?StringTrimRight("\2",1):StringTrimRight("\2",2)))'
$sStr2Newa = StringRegExpReplace($sStr2a, $REPattern, $REReplacement)
;ConsoleWrite($sStr2Newa & @CRLF)
ConsoleWrite(Execute($sStr2Newa) & @CRLF)

ConsoleWrite(" ---- Ans 2b ------" & @CRLF)
Local $sStr2b = "helloHelloBye192.168.1.1:123418292293hdhfhdbdabgsgs"
Local $sStr2Newb = StringRegExpReplace($sStr2b, $REPattern, $REReplacement)
;ConsoleWrite($sStr2Newb & @CRLF)
ConsoleWrite(Execute($sStr2Newb) & @CRLF)

ConsoleWrite(" ---- Ans 2c ------" & @CRLF)
$sStr2c = "helloHelloBye192.168.1.1:723418292293hdhfhdbdabgsgs"
$sStr2Newc = StringRegExpReplace($sStr2c, $REPattern, $REReplacement)
;ConsoleWrite($sStr2Newc & @CRLF)
ConsoleWrite(Execute($sStr2Newc) & @CRLF)

ConsoleWrite(" ---- Ans 3 ------" & @CRLF)
$sStr3 = "Bugs:Hello" & @CRLF & "Bugs:Hello" & @CRLF & "Bugs:Hello"
ConsoleWrite(StringRegExpReplace(StringRegExpReplace($sStr3, '((.+)\R?)+', '\1'), '(.+:)', '') & @CRLF)

 

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