Jump to content

cold anyone help me with regex replace?


E1M1
 Share

Recommended Posts

I am trying to replace occurrence with it's number but I can't seem to find way to do this.

ConsoleWrite(StringRegExpReplace("blah blah {text4} blahh {text} blahh {text12} and blah {text2}",'({(text(d?|d+))})',"{d}")&@CRLF)

I tried various replacements but didn't find suitable one.

Output I want to get would be:

blah blah 1 blahh 2 blahh 3 and blah 4

current code outputs blah blah {d} blahh {d} blahh {d} and blah {d} . I would like to know if there is way to tell StringRegExpReplace that I want matched occurrence number instead of {d}? OR do I have to use for loop with StringReplace ( 'blah blah {d} blahh {d} blahh {d} and blah {d}', "{d}", $i, $i,$i) in it?

Thanks in advance

Edited by E1M1

edited

Link to comment
Share on other sites

  • Moderators

E1M1,

Does this do what you want? :)

$sString = "blah blah {text4} blahh {text1} blahh {text12} and blah {text2}"

$sNewString = StringRegExpReplace($sString, "{text(d+)}", "$1")

ConsoleWrite($sNewString & @CRLF)

If not explain why not and we can play with it until it does. ;)

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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

not elegant but perhaps?

$x = 0
$nOffset = 1
While 1
    $array = StringRegExp('blah blah {text4} blahh {text} blahh {text12} and blah {text2}', '(?i)(?s)([a-zs]*)s*({textd*})', 1, $nOffset)

    If @error = 0 Then
        $nOffset = @extended
    Else
        ExitLoop
    EndIf
    For $i = 0 To UBound($array) - 1
        If StringInStr($array[$i], '{') Then
            $x = $x + 1
            $array[$i] = $x
        EndIf
        ;MsgBox(0, "RegExp Test with Option 1 - " & $i, $array[$i])

        ConsoleWrite($array[$i] & " ")
    Next
WEnd
        ConsoleWrite(@CRLF)

Jury

Edited by Jury
Link to comment
Share on other sites

What about this?:

$s = "blah blah {text4} blahh {text} blahh {text12} and blah {text2}"
$a = StringRegExp($s, "blah+", 3)
$o = $a[0] & " "
For $i = 1 To UBound($a) - 1
    $o &= $a[$i] & " " & $i & " "
Next
ConsoleWrite($o & @LF)

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Here are two methods that appear to work.

; ----------------------- Method 1 ---------------------------------------------------------
Local $nOffset = 1, $iCount = 1, $sStr = 'blah blah {text4} blahh {text} blahh {text12} and blah {text2}'

While 1
    $array = StringRegExp($sStr, '(?i)(?s)([a-zs]*)s*({textd*})', 1, $nOffset)
    If @error = 0 Then
        $nOffset = @extended
    Else
        ExitLoop
    EndIf
    $sStr = StringRegExpReplace($sStr, "(.{" & $nOffset - StringLen($array[1]) - 1 & "})(" & $array[1] & ")", "1 " & $iCount)
    $iCount += 1
WEnd
ConsoleWrite(StringStripWS($sStr, 7) & @CRLF)


; ----------------------- Method 2 ---------------------------------------------------------
Local $sStr = 'blah blah {text4} blahh {text} blahh {text12} and blah {text2}'

$sStr = Execute("'" & StringRegExpReplace($sStr, "({textd*})", " ' & MyFunc() & '") & "'")

ConsoleWrite(StringStripWS($sStr, 7) & @CRLF)


Func MyFunc()
    Static $a = 0
    $a += 1
    Return $a
EndFunc   ;==>MyFunc
Edited by Malkey
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...