Jump to content

Replace text that does not match a givin RegExp


EKY32
 Share

Go to solution Solved by jguinch,

Recommended Posts

Hello, I have a text that I need to do some replacment to it's characters, lets say I need to replace the dashes "-" with spaces, but I do not want the dashes inside the anchor tag to be replaced, only the ones outside it.

$text = "here is the link http://www.my-li-nk.com that should stay as is, and here are-the-dashes-that-need-to-be replaced with spaces"

I've previously used >somoke_n's code to identify the anchor tag.

Func _replaceURLWithHTMLLinks($sText)
    Local Const $sExp = "(?i)(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])"
    Return StringRegExpReplace($sText, $sExp, "<a href='$1'>$1</a>")
EndFunc

Thanks in advance.

Edited by EKY32

[font="'trebuchet ms', helvetica, sans-serif;"]Please mark the answer of your question if you found it.[/font]

Link to comment
Share on other sites

Thanks, I don't think this helps in my situation, In fact, I do not know the real URL, I'm filtering a long amount of text, converting the plain text URLs to real HTML coded ones, and then replace some other characters (like smiles for example..) with image codes .. so I do not want the smile filtering function to approach to the URLs and replace any of their text.

Thanks.

Edited by EKY32

[font="'trebuchet ms', helvetica, sans-serif;"]Please mark the answer of your question if you found it.[/font]

Link to comment
Share on other sites

  • Moderators

1.  get an array of urls with regex

2.  replace urls with unique strings that also represent the index of the url in your array

3.  replace all the characters you need to

4.  replace the unique strings with the array of urls based on index

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

EKY32.

How about this: ;)

$sFiller = "###"

$sText = "here is the link http://www.my-li-nk.com that should stay as is, and here are-the-dashes-that-need-to-be replaced with spaces"

; Extract the link
$sExtract = StringRegExpReplace($sText, "^.*(http\S*)\s.*$", "$1")

; Replace the link by the filler, get rid of the dashes and then replace the link
$sNewText = StringReplace(StringReplace(StringReplace($sText, $sExtract, $sFiller), "-", " "), $sFiller, $sExtract)

ConsoleWrite($sNewText & @CRLF)
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

SmOke_N's way can be something like this :

#Include <Array.au3> ; just for _ArrayDisplay

$text = "here is the link http://www.my-li-nk.com that should stay as is, and here are-the-dashes-that-need-to-be replaced with spaces."

; Extract all links
$aLinks = StringRegExp($text, "(?i)(\b(?:https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])", 3)
_ArrayDisplay($aLinks, "All links") 

; Replace all links by a mark : <link{x}>
For $i = 0 To UBound($aLinks) - 1
    $text = StringReplace($text, $aLinks[$i], "<link{" & $i & "}>", 1)
Next
ConsoleWrite($text & @CRLF)

; replace all dashes between two words by a blank
$text = StringRegExpReplace($text, "(?<=\w)-(\w+)", " $1")
ConsoleWrite($text & @CRLF)

; Replace each mark by its variable name
$text = StringReplace($text, '"', '""')
$text = StringRegExpReplace('"' & $text & '"', "<link\{(\d+)\}>", '" & \$aLinks[$1] & "')
ConsoleWrite($text & @CRLF)

; Replace each variable by its value
$text = Execute($text)
ConsoleWrite($text & @CRLF)
Link to comment
Share on other sites

You people are all geniuses, i'm very thankful to all of you. All the methods worked fine but the one jguinch posted was perfect to my need.

Thank you all again. :king:

[font="'trebuchet ms', helvetica, sans-serif;"]Please mark the answer of your question if you found it.[/font]

Link to comment
Share on other sites

And another one.

Local $sText = "here is the link http://www.my-li-nk.com and another http://www.his-li-nk.com that should stay as is, and here are-the-dashes-that-need-to-be replaced with spaces"

Local $sStr = '"' & StringRegExpReplace($sText, "([\w.,/:]+(?:\-[\w.,/:-]+))", '" & (StringRegExp("${1}","^http") ? "${1}" : (StringRegExpReplace("${1}","-"," ")))  & "') & '"'

;ConsoleWrite($sStr & @LF)
ConsoleWrite(Execute($sStr) & @LF)
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...