Jump to content

StringRegExpReplace to correct HTML tag


Go to solution Solved by PhoenixXL,

Recommended Posts

Hi all

I get a problem with StringRegExpReplace 

Here is the code:

$string = "< / li  >  < /  li  >  < /li > < / li> < / li  > </ li  >"
$array = StringRegExp($string, "<(.*)/(.*)(li)(.*)>", 3)
_ArrayDisplay($array)

I want to replace all with </li>, and the final result is:

</li>  </li>  </li> </li> </li> </li>

Please help, thanks

Link to comment
Share on other sites

  • Moderators

luckyluke,
 
Why use an SRER?  You can do it very simply like this: :)

$sString = "< / li  >  < /  li  >  < /li > < / li> < / li  > </ li  >"

; Replace all spaces and then add one between the ><
$sNewString = StringReplace(StringReplace($sString, " ", ""), "><", "> <")

ConsoleWrite($sNewString & @CRLF)

Good enough?  Or is there something you have not told us? :huh:

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

  • Solution

The requirement sounds incomplete. I hope there isn't any more possibilities you have left.

Here is what you ask

;working - match anything inbetween "<~li~>" , and replace everything with a "</li>"
$string = StringRegExpReplace("< / li  >  < /  li  >  < /li > < / li> < / li  > </ li  >", "<[^>/]*/[^>]*?li[^>]*>", "</li>")
MsgBox( 64, "", $string)

Regards :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

luckyluke,

 

Why use an SRER?  You can do it very simply like this: :)

$sString = "< / li  >  < /  li  >  < /li > < / li> < / li  > </ li  >"

; Replace all spaces and then add one between the ><
$sNewString = StringReplace(StringReplace($sString, " ", ""), "><", "> <")

ConsoleWrite($sNewString & @CRLF)

Good enough?  Or is there something you have not told us? :huh:

M23

In the first, it will not work any more will i use stringreplace in a HTML code, eg:

<BR>new ControlPanel 
<LI>perfect . < / Li >
<LI>perfect Taskbar Button Position . < / Li >Improved 
<LI>StartMenu . < / Li >
<LI>Improved maximum , minimum , close button . < / Li >
<LI>New Improved Taskbar Improved 
<LI><A href="" target=_blank>. Control Panel </A>View 
<LI>Improved address bar 
<LI>Adressbar Improved text size . . . < / Li >
<LI>Adressbar Glow Improved Text 
<UL></UL><BR>

The requirement sounds incomplete. I hope there isn't any more possibilities you have left.

Here is what you ask

;working - match anything inbetween "<~li~>" , and replace everything with a "</li>"
$string = StringRegExpReplace("< / li  >  < /  li  >  < /li > < / li> < / li  > </ li  >", "<[^>/]*/[^>]*?li[^>]*>", "</li>")
MsgBox( 64, "", $string)

Regards :)

I think this is i wanted. Stringregexp is so difficult to learn. I will test this with other HTML tag

Thank you very much!

Link to comment
Share on other sites

  • Moderators

LuckyLuke,

So as both PhoenixXL and I suspected there was more to your request that you initially stated. In future, please make your questions clear from the beginning - then we do not waste time producing code which does not fill the actual requirement. ;)

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

The requirement sounds incomplete. I hope there isn't any more possibilities you have left.

Here is what you ask

;working - match anything inbetween "<~li~>" , and replace everything with a "</li>"
$string = StringRegExpReplace("< / li  >  < /  li  >  < /li > < / li> < / li  > </ li  >", "<[^>/]*/[^>]*?li[^>]*>", "</li>")
MsgBox( 64, "", $string)

Regards :)

 

I just tested this simpler version which works fine too.
I'm not RegExp guru so I don't know if this my simpler solution has some disadvantages or bugs
 
$string = "a <1 /2 li 3 > 4 < /  li 5 > 6 < /li > < / li> < / li  > </ li  > < / li  >  < /  li  >  < /li > < / li> < / li  > </ li  >"
$string = StringRegExpReplace($string, "(<.*?/.*?li.*?>)", '</li>')
ConsoleWrite($string& @CRLF)
Link to comment
Share on other sites

@Zedna,

Your method doesn't have any bugs, it will work as expected. Still using lazy operators will cause the engine to have a lot of back-tracking( at least in this scenario ). Therefore I prefer greedy operator.

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

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

×
×
  • Create New...