Jump to content

Find a expression after a specific text


Recommended Posts

Hi Folks,

I had been searching online for some help but failed. I would like to my script to find the word after a specific word, but my script it does not return the field after it.

My scripts look something like this:

$FileName = @ScriptDir & "\TEST.txt"

$Backup = @ScriptDir & "\TEST-BCK.txt"

FileCopy ($FileName, $Backup)

$FileContents = FileRead($FileName)

$Results = StringSplit( "Password", ":",2)

Msgbox(0,"Status","Password: " & $Results)

What I wish to achieve is that it would search for this string call password in this particular file and then it would return the filed after it.

The text file looks something like this:

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

Name : Test123

Application : MS Outlook 2002/2003/2007

Email : test123@isp.com.sg

Server : pop.isp.com.sg

Type : POP3

User : user123

Password :blink:@ssw0rd

Profile : Outlook

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

Any help would be appreciated, thank you in advance.

Edited by dion
Link to comment
Share on other sites

You could try searching between the ":" and the character after the password- maybe something like this?:

$FileName = @ScriptDir & "\TEST.txt"
$Backup = @ScriptDir & "\TEST-BCK.txt"

FileCopy ($FileName, $Backup)
$FileContents = FileRead($FileName)

$passwordloc = StringinStr($FileContents, "Password"); finds the number of the first character of password
$passwordfinloc = $passwordloc + 9 ;find the first character after "Password:"
$profileloc = StringinStr($FileContents, "Profile")
$userpassword = StringMid($FileContents, $passwordfinloc, $profileloc - $passwordfinloc) ;finds the characters between the two
Link to comment
Share on other sites

$sBuffer="Name : Test123"&@CRLF& _
"Application : MS Outlook 2002/2003/2007"&@CRLF& _
"Email : test123@isp.com.sg"&@CRLF& _
"Server : pop.isp.com.sg"&@CRLF& _
"Type : POP3"&@CRLF& _
"User : user123"&@CRLF& _
"Password :P@ssw0rd"&@CRLF& _
"Profile : Outlook"
$aMatch=StringRegExp($sBuffer,"Password\s*\:\s*(.*)",1)
If Not @error Then ConsoleWrite("Match:"&$aMatch[0]&@CRLF)

*edit: code tags

Edited by Ascend4nt
Link to comment
Share on other sites

Thanks folks, I had tried and it works. Thank you so much.

Ascend4nt>> I did not use your as I do not understand it, my apologies as I'm very new with scripting. Could I ask what is the usage \s* ?

Link to comment
Share on other sites

Thanks folks, I had tried and it works. Thank you so much.

Ascend4nt>> I did not use your as I do not understand it, my apologies as I'm very new with scripting. Could I ask what is the usage \s* ?

In a regular expression it means match any spaces that may or may not appear before and after the colon. It would match any of the follwing plus any where multiple spaces were used in the same positions

  • Password:mypassword
  • Password: mypassword
  • Password : mypassword
  • Password :mypassword

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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