E1M1 Posted January 8, 2012 Share Posted January 8, 2012 (edited) 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 January 8, 2012 by E1M1 edited Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 8, 2012 Moderators Share Posted January 8, 2012 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Jury Posted January 8, 2012 Share Posted January 8, 2012 (edited) 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 January 8, 2012 by Jury Link to comment Share on other sites More sharing options...
UEZ Posted January 9, 2012 Share Posted January 9, 2012 (edited) 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 January 9, 2012 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 More sharing options...
Malkey Posted January 9, 2012 Share Posted January 9, 2012 (edited) 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 January 9, 2012 by Malkey Link to comment Share on other sites More sharing options...
UEZ Posted January 9, 2012 Share Posted January 9, 2012 @Malkey: method 2 is coooool Br, 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now