Jump to content
Sign in to follow this  
luckyluke

StringRegExpReplace to correct HTML tag

Recommended Posts

luckyluke

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

Share this post


Link to post
Share on other sites
Melba23

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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
PhoenixXL

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

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.

Share this post


Link to post
Share on other sites
luckyluke

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!

Share this post


Link to post
Share on other sites
Melba23

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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
luckyluke

Melba23

Problem is solved, thanks.

Share this post


Link to post
Share on other sites
Zedna

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

Share this post


Link to post
Share on other sites
PhoenixXL

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

Share this post


Link to post
Share on other sites
AZJIO

; $sStr = StringRegExpReplace("< / li  >  < /  li  >  < /li > < / li> < / li  > </ li  >", "<\s*/\s*li\s*>", "</li>")
$sStr = StringRegExpReplace("<   li  >  < /li > <  li> < / li  > <  li  >  </ li  >", "(<)\s*(/)?\s*(li)\s*(>)", "\1\2\3\4")
MsgBox( 64, "", $sStr)

  • Like 1

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  

  • Similar Content

    • ur
      By ur
      Is there any UDF to remove all anchor tags <a> with a particular class (and also its sub elements completely) in a html document.
      Here the classes are browse and breadcrumbs
      Like in the below image.


       
      I am not able to find that option in IE.au3
       
      Please suggest.
    • milkmoron
      By milkmoron
      I am trying to automate something in a web browser but i need some help with finding the html code to a web applet. How do I access the code.
    • luckyluke
      By luckyluke
      $t = '... 1-347-318-9643 1-347-318-9647 1-347-318-9648 1-347-318-9650 1-347-318-9651 1-347-318-9652 1-347-318-9653 1-347-318-9655 1-347-318-&nbsp;...' $pattern = '347.*?318.*?9655' $tmp = StringRegExpReplace($t, $pattern, "|||", 1) ConsoleWrite($tmp & @CRLF) However i got this output:
      ... 1-|||  1-347-318-&nbsp;...
      Why i got only that, where is the other string, i thought the output should be this:
      ... 1-347-318-9643  1-347-318-9647  1-347-318-9648  1-347-318-9650  1-347-318-9651  1-347-318-9652  1-347-318-9653  1-|||  1-347-318-&nbsp;...
    • Seminko
      By Seminko
      Is there a way to grab non-hardcoded but rather javascript generated data from a webpage?
      Tried a get request as well as _IEBodyReadHTML but both seem to grab the code without the javascript generated data.
      $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", "link", False) $oHTTP.Send() $oReceived = $oHTTP.ResponseText $oStatusCode = $oHTTP.Status Global $DataArray[10][5] If $oStatusCode <> 200 Then Exit MsgBox(1, "Error", "Status Code <> 200") EndIf FileWrite(@ScriptDir & "\output.txt", $oReceived) ; //////// #include <IE.au3> Local $FullLink = "link" Local $oIE = _IECreate($FullLink, 0, 0) _IELoadWait($oIE) Local $sText = _IEBodyReadHTML($oIE) FileWrite(@ScriptDir & "\output.txt", $sText)  
    • SkysLastChance
      By SkysLastChance
       
      WinActivate("MEDITECH - Internet Explorer") Sleep (500) $oIE = _IEAttach("MEDITECH") $oDiv1 = _IEGetObjById($oIE, "sysmenu-searchbarbutton") _IEAction($oDiv1, "click") I am just trying to click the little magnifying glass, next to the gear button with no luck. I was hoping someone might have an idea why this is not working?
       

×