Jump to content

Help needed in replace strings in expression


Go to solution Solved by kylomas,

Recommended Posts

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

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

  • Solution

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

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

  • Moderators

lolipop,

Try this: :)

$sOutput = StringRegExpReplace($sInput, '^(.*<value>)(.*)(</value>.*)$', '$1hello$3')
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

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 by lolipop
Link to comment
Share on other sites

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

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

  • Moderators

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 $3
Clearer now? :)

M23

Edit: Spot on kylomas. :thumbsup:

And I am by no means expert. ;)

Edited by Melba23

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

  • Moderators

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

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! :D

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

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