Jump to content

StringReplace() question


blumi
 Share

Recommended Posts

I have a script, which Imports some text, does some changes in the text, formating, colour changing, date format change etc. etc.

All works fine, but I have one problem.

In the text there can be some entries with the following words. "SHORT" and "STAY SHORT".

For Example

Our system's recommendation today is to STAY SHORT. The previous SHORT signal was issued on 30.08.2018, 1 day ago, when the stock price was 43.9250. Market attention is now on the downside. SHORT is a dangerous signal. Sudden increases in prices can lead to huge losses. For this reason stop loss levels must be kept in mind at all times and SHORT orders must never be placed without a stop loss
 

What I have tried was to write a function like this.

Func MacheHTML($string)

    $rot    = "#ff5050"
    $orange = "#ff9933"

    ; Alles was orange wird
        $string = StringReplace($string, "STAY SHORT", "<font color=" & $orange & ">STAY SHORT</font>",0,1)

    ; Alles was rot wird
        $string = StringReplace($string, "SHORT", "<font color=" & $rot & ">SHORT</font>",0,1)
    
    Return $string
EndFunc

The problem here is I don't get it to work, that "STAY SHORT" ist complete in orange and all "SHORT"s are red.

Only a mix of red/orange or no colour Change etc.

Can't see the forest for the trees it seems, need some ideas how to handle it.

Thanks

Link to comment
Share on other sites

Hi.

It's propably possible with some nice RegEx, the simple approach is that one:

  • Do your replace for SHORT only
  • then correct the mixed up coloring for "STAY SHORT"
$MixedUpStayShort="STAY <font color=" & $rot & ">SHORT</font>"
$FixedStayShort="<FONT COLOR=" & $orange & ">STAY SHORT</font>"

StringReplace($string,$MixedUpStayShort,$FixedStayShort)

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

But the string can contain STAY and STAY SHORT, how to handle the script to know when there is only SHORT to make it red and if there is a STAY SHORT to make it orange.

One problem I have STAY oder STAY SHORT can be in the string more than one time.

 

Link to comment
Share on other sites

Hi.

Take a StringRegExpReplace for the SHORT part. Use the search string „(?i)(?<!stay )short“ to only color SHORT in red but not STAY SHORT. After that you can replace STAY SHORT with the orange part.

You can see the result and do some testing with the regex here: https://regex101.com/r/wCaBO4/1

Simpel

P.S. If you use short and stay short in lower letters and they should not be colored then delete that (?i) part from the search string as this means searching case insensitive.

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

$string = StringRegExpReplace($string, '(?<!STAY )SHORT', "<FONT COLOR=" & $rot & ">SHORT</font>")
    $string = StringReplace($string, "STAY SHORT", "<font color=" & $orange & ">STAY SHORT</font>",0,1)

With these lines in the function it works fine.

All SHORTS are red, all STAY SHORTS are orange

Thank you

Edited by blumi
Link to comment
Share on other sites

Or without RegEx:

Func MacheHTML($string)

    $rot    = "#ff5050"
    $orange = "#ff9933"


    $string = StringReplace($string, "STAY SHORT", "STAY S_HORT",0,1)


    ; Alles was orange wird
        $string = StringReplace($string, "STAY S_HORT", "<font color=" & $orange & ">STAY S_HORT</font>",0,1)

    ; Alles was rot wird
        $string = StringReplace($string, "SHORT", "<font color=" & $rot & ">SHORT</font>",0,1)

    $string = StringReplace($string, "STAY S_HORT", "STAY SHORT",0,1)


    Return $string
EndFunc

 

Rule #1: Always do a backup         Rule #2: Always do a backup (backup of rule #1)

Link to comment
Share on other sites

7 minutes ago, ajag said:

Or without RegEx:

Func MacheHTML($string)

    $rot    = "#ff5050"
    $orange = "#ff9933"


    $string = StringReplace($string, "STAY SHORT", "STAY S_HORT",0,1)


    ; Alles was orange wird
        $string = StringReplace($string, "STAY S_HORT", "<font color=" & $orange & ">STAY S_HORT</font>",0,1)

    ; Alles was rot wird
        $string = StringReplace($string, "SHORT", "<font color=" & $rot & ">SHORT</font>",0,1)

    $string = StringReplace($string, "STAY S_HORT", "STAY SHORT",0,1)


    Return $string
EndFunc

 

First I thoght you have a type (S_HORT). Nice idea.

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