lolipop Posted August 14, 2013 Share Posted August 14, 2013 Hi guys, Need some help to this. I need to replace the value in red to some other value/text. The problem is the value in red can be either empty or it can also be other text or characters. It is not always "qwerty". So how can I do a search condition for this and then replace it with text/value of my choice? TIA <prop oor:name="givenname" oor:op="fuse"><value>qwerty</value></prop> Link to comment Share on other sites More sharing options...
Starg Posted August 14, 2013 Share Posted August 14, 2013 (edited) Try StringRegExpReplace. Edited August 14, 2013 by Starg Link to comment Share on other sites More sharing options...
lolipop Posted August 14, 2013 Author Share Posted August 14, 2013 Thanks. But I can't seem to make it work. I think my expression is very wrong. Sample code Test1() Func Test1() Local $sInput = '<prop oor:name="givenname" oor:op="fuse"><value>qwerty 123 !@#</value></prop>' Local $sOutput = StringRegExpReplace($sInput, '<prop oor:name="givenname" oor:op="fuse"><value>[:alnum:][:alpha:][:ascii:][:blank:][:cntrl:][:digit:][:graph:][:lower:][:print:][:punct:][:space:][:upper:][:word:][:xdigit:]</value></prop>', 'hello') Display($sInput, $sOutput) EndFunc Func Display($sInput, $sOutput) ; Format the output. Local $sMsg = StringFormat("Input:\t%s\n\nOutput:\t%s", $sInput, $sOutput) MsgBox(0, "Results", $sMsg) EndFunc Link to comment Share on other sites More sharing options...
Solution kylomas Posted August 14, 2013 Solution Share Posted August 14, 2013 lolipop, Try this local $str = '<prop oor:name="givenname" oor:op="fuse"><value>qwerty</value></prop>' $str = stringregexpreplace($str,'(<value>)([^<]*)(</value)','$1' & ' some other string ' & '$3') ConsoleWrite('! ' & $str & @LF) kylomas lolipop 1 Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 14, 2013 Moderators Share Posted August 14, 2013 lolipop,Try this: $sOutput = StringRegExpReplace($sInput, '^(.*<value>)(.*)(</value>.*)$', '$1hello$3')M23 lolipop 1 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...
lolipop Posted August 14, 2013 Author Share Posted August 14, 2013 (edited) Hi kylomas & Melba23, Thank you very much both for the assistance. Both expression works very well. Do you mind explaining to me the expression in detail cos I don't quite understand some of it. 1st For this ([^<]*) I understand that [^]* means Match any character not in the set and the * means Repeat the previous character. But not this [^<] ? I can't seem to find [^<] explaination. 2nd Why is there a need to put in the $1 and $3? Sorry Melba23 Your expression '^(.*<value>)(.*)(</value>.*)$' is a bit more difficult for me to understand. Could you kindly elaborate this? Thank you all. Edited August 14, 2013 by lolipop Link to comment Share on other sites More sharing options...
kylomas Posted August 14, 2013 Share Posted August 14, 2013 lolipop, (<value>) first capturing group ([^<]*) second capturing group - capture anything that is not a "<", repeat set 0 or more times (</value) third capturing group $1 and $3 correspond to first and third capturing group I am no where near the expert that M23 is but I'll try to explain his pattern ^ start of string (.*<value>) first caputuring group - capture everything up to and including <value> (.*) second capturing group - capture any single char, repeat 0 or more times (</value>.*) third caputuring group capture <value> and everything after till $ end of string kylomas lolipop 1 Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 14, 2013 Moderators Share Posted August 14, 2013 (edited) lolipop, ^ - Start at the beginning of the string and ... (.*<value>) - ...capture a group up to and including <value> - this is stored as $1 (.*) - Capture a group of any number of characters (including none at all) up to the beginning of the next group - this is stored as $2 (</value>.*) - Capture a group (stored as $3) from </value> to... $ - ...the end of the string $1hello$2 - Replace it (ie the whole thing as we have capture all of it) with group $i, "hello" and group $3Clearer now? M23Edit: Spot on kylomas. And I am by no means expert. Edited August 14, 2013 by Melba23 lolipop 1 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...
kylomas Posted August 14, 2013 Share Posted August 14, 2013 @M23 - Whew...was sweating that one! Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
lolipop Posted August 14, 2013 Author Share Posted August 14, 2013 Thank you both for taking the time to write the explaination. Yes, it's very clear now. Definitely better and clearer than the help file. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 14, 2013 Moderators Share Posted August 14, 2013 lolipop,SREs are complicated beasts and need a lot more than the limited page the AutoIt Help file can offer. I often say that they are the hardest thing in computing terms I have ever tried to learn - and I stand in awe of the real gurus around here. I recommend this site as a good place to start learning about them - one of the gurus always suggests here. Your choice. Good luck - and do not blame us when your brain begins to bleed! M23 lolipop 1 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...
lolipop Posted August 14, 2013 Author Share Posted August 14, 2013 Good luck - and do not blame us when your brain begins to bleed! I will try to brush up my skills from those sites. Thanks Melba PS: No worry on the bleeding part. Hospital is very near my house. 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