Jump to content
Sign in to follow this  
Suppir

StringRegExpReplace. Problem with cutting out founded

Recommended Posts

Suppir

Hello!

I have the code:

for $i = 0 to UBound($Search) - 1

$Text = StringRegExpReplace($Text, $Search[$i][0], $Search[$i][1])

Next

$Search[$i][0] - is a regular exression to search

$Search[$i][0] - is a text to replace founded

How to cut out the founded if my $Search[$i][1] defined but equals "nothing"? (not assigned any value)

In this situation replace does not work.

Edited by Suppir

Share this post


Link to post
Share on other sites
Suppir

Try

$Text = StringRegExpReplace($Text, $Search[$i][0], "'" & $Search[$i][1] & "'")

In this situation script prints many many quotes '

Share this post


Link to post
Share on other sites
Suppir

Maybe

If $Search[$i][1] = "" Then 
    $Text = StringRegExpReplace($Text, $Search[$i][0], '')
Else
    $Text = StringRegExpReplace($Text, $Search[$i][0], $Search[$i][1])
EndIf

I've try it before but it does not work too. In this situation replace does not work at all. Edited by Suppir

Share this post


Link to post
Share on other sites
Malkey

Hello!

I have the code:

for $i = 0 to UBound($Search) - 1

$Text = StringRegExpReplace($Text, $Search[$i][0], $Search[$i][1])

Next

$Search[$i][0] - is a regular exression to search

$Search[$i][0] - is a text to replace founded

How to cut out the founded if my $Search[$i][1] defined but equals "nothing"? (not assigned any value)

In this situation replace does not work.

Local $sRes
Local $sText = " The cat sat on the mat."
Local $aSearch[4][2] = [["cat", "dog"],["the", "a"],["mat", "rug"],["sat ", ""]]

$sRes = $sText & @CRLF

For $i = 0 To UBound($aSearch) - 1
    $sText = StringRegExpReplace($sText, $aSearch[$i][0], $aSearch[$i][1])
    $sRes &= $sText & @CRLF
Next

MsgBox(0, "StringRegExpReplace", $sRes)
;ConsoleWrite($sRes & @CRLF)

The expected results obtained from this example is:-

The cat sat on the mat.

The dog sat on the mat.

The dog sat on a mat.

The dog sat on a rug.

The dog on a rug.

Note - The default, case-sensitivity, is working. eg. Match on "the" but no match on "The"

I hope this example helps you realize why your script is not performing as you expect.

Share this post


Link to post
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
Sign in to follow this  

×