Jump to content
Sign in to follow this  
lolipop

Help needed in replace strings in expression

Recommended Posts

lolipop

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>

Share this post


Link to post
Share on other sites
lolipop

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

Share this post


Link to post
Share on other sites
kylomas

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

  • Like 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

Share this post


Link to post
Share on other sites
Melba23

lolipop,

Try this: :)

$sOutput = StringRegExpReplace($sInput, '^(.*<value>)(.*)(</value>.*)$', '$1hello$3')
M23
  • Like 1

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

 

Share this post


Link to post
Share on other sites
lolipop

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

Share this post


Link to post
Share on other sites
kylomas

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

  • Like 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

Share this post


Link to post
Share on other sites
Melba23

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
  • Like 1

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

 

Share this post


Link to post
Share on other sites
kylomas

@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

Share this post


Link to post
Share on other sites
lolipop

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

Share this post


Link to post
Share on other sites
Melba23

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

  • Like 1

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

 

Share this post


Link to post
Share on other sites
lolipop

Good luck - and do not blame us when your brain begins to bleed! :D

 

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

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  

×