Jump to content

String replace wild card


Recommended Posts

I would like to replace everything after "|" with "" How can I do this.

I have this so far. I am looking for a "Wild Card" such as "*" I can not depend on trim since the strings are not common, and vary in length and placement of "|"

So really all I want to keep is everything before "|"

MsgBox(64, "OK ",StringReplace(GUICtrlRead(GUICtrlRead($listviewID)), "|", ""))

Thanks!

Edited by Hatcheda
Link to comment
Share on other sites

  • Moderators

StringRegExpReplace($sString, "\|.*?", "")

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

StringRegExpReplace($sString, "\|.*?", "")

Maybe that should be

$Ans = StringRegExpReplace($sString, "\|.*", "") & '|'

or if you don't want the '|' included then

$Ans = StringRegExpReplace($sString, "\|.*", "")

ANother way is

$Ans = stringleft($sString,stringinstr($sString,'|'))

or, again, if you don't want the '|' then

$Ans = stringleft($sString,stringinstr($sString,'|') - 1)
Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • 3 years later...

Sorry for the resurrection of an old post.....

I'm no good at RegEx, and I wrote this to get around it using StringReplace. Hope this makes sense. Might not be the best looking piece of code, but it does what the OP asks.

$string = "This is the text I want to keep|This is the text I want to delete"
$findText = "|*"
$replaceText = ""
$newString = StringReplace($string,WildCardFindText($findText,$string),$replaceText)
MsgBox(0,"New",$newString)
Func WildCardFindText($findText, $string)
If StringInStr($findText,"*") > 0 Then
  $WildCardPosition = StringInStr($findText,"*")
  If $WildCardPosition = StringLen($findText) Then
   $findText = StringLeft($findText,(StringLen($findText)-1))
   $findText = StringRight($string,(StringLen($string)-StringInStr($string,$findText))+1)
  Else
   $findText = StringRight($findText,(StringLen($findText)-1))
   $findText = StringLeft($string,(StringLen($string) - (StringLen($string)-StringInStr($string,$findText))))
  EndIf
EndIf
Return $findText
EndFunc
Edited by mike9900
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...