Jump to content

Recommended Posts

Posted (edited)

Greetings,

The script below will read the testfile.txt, that has the following contents:

path=c:\windows

It will then proceed to strip any text to the left of the equal sign including the equal sign i.e. so it shows up as c:\windows, which is the expected behavior. Alternately, I would like to strip any text that is right of the equal sign including the equal sign instead. Whether I change the "StringTrimLeft" to "StringTrimRight" or change the "1"/"-1" entries on the "$LeftofEqual" variable or both, I don't get the hopeful results of it showing up as path

I can't do any specific trim numbers as that will vary, so that's why I am acquiring the number of characters leading up to the equal sign, but I can't seem to reverse it where it acquires the number of characters from the right side of it instead. The full code is below:

$readline = FileReadLine("testfile.txt",1)
$LeftofEqual=StringInStr($readline, "=", 1, -1)
$result = StringTrimLeft ($readline,$LeftofEqual)
MsgBox(0,"",$result

Thanks in advance for any assistance that can be provided.

Edited by TheCrimsonCrusader
Posted (edited)

Here you go:

$readline = FileReadLine("testfile.txt",1)
$LeftofEqual=StringInStr($readline, "=", 1, -1)
$result = StringTrimLeft ($readline,$LeftofEqual)
MsgBox(0,"",$result)
$result = StringTrimRight($readline, StringLen($readline)-$LeftofEqual+1)
MsgBox(0,"",$result)

To strip the right part - you need to consider string length.

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

  • Moderators
Posted

TheCrimsonCrusader,

Or you can use a SRE like this to split on the = sign directly: :)

$sText = "path=c:\windows"

$sLeft  = StringRegExpReplace($sText, "(.*)=(.*)", "$1")
$sRight = StringRegExpReplace($sText, "(.*)=(.*)", "$2")

MsgBox(0, "Result", $sLeft & @CRLF & $sRight)

SRE explanation:

(.*)    - Capture any number of characters
=       - Do not capture an = sign
(.*)    - Capture any number of characters

$1      - Replace with first captured group
$2      - Replace with second captured group

Please ask if anything is unclear. :mellow:

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

 

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