Jump to content

Replace in String using Wildcards


Recommended Posts

Whats up?

I need your help again...

Basically, I need to replace this string with another but using wildcards (not really wildcards): '<a href="*ANY KIND OF CHARACTERS HERE*">'.

In Example:

$string1 = '<b>Some text:</b></br></br><a href="http://127.0.0.1">THIS A LINK</a></br></br><b>The End.</b>'
$string2 = ReplaceIt($string1, '<a href="*anything*">THIS A LINK</a>', "THIS IS SOME UPPERCASE TEXT", 0, 0)
; The function above does not exists, just used to show the example. The last two paremeters are the "occurence" and "casesense" respectively.
; The *anything* is any string that contains any char within any len. of chars

$fileLocation = @DesktopDir & "\test1.htm"
FileDelete($fileLocation)
FileWrite(FileOpen($fileLocation), $string2)

Some text:

THIS IS A LINK

The End.

would become:

Some text:

THIS IS SOME UPPERCASE TEXT

The End.

I've searched the forums already but nothing exactly fits to what I need.

Hope you can help me again guys :idea:

Thanks in advance.

Link to comment
Share on other sites

If <a at the start and a> at the end are constant you could do it like this.

$string1 = '<b>Some text:</b></br></br><a href="http://127.0.0.1">THIS A LINK</a></br></br><b>The End.</b>'
;~ $string2 = ReplaceIt($string1, '<a href="*anything*">THIS A LINK</a>', "THIS IS SOME UPPERCASE TEXT", 0, 0)
$sYourString = "THIS IS SOME UPPERCASE TEXT"
$sLeft = StringLeft($string1, StringInStr($string1, "<a")-1)
$sRight = StringRight($string1, StringLen($string1)-(StringInStr($string1, "a>")+1))
ConsoleWrite($sLeft & $sYourString & $sRight & @CR)
GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.
Link to comment
Share on other sites

I learned regex using Expresso. Try it out, you might like it.

Yes I know that, but I wanna make it entirely on AutoIt 'cuz the string will be dynamic.

What I want to do:

- Get the sourcecode from a page and save it to a file each 5 minutes: done.

- Split the code and get only some regions of it: done.

- Remove all the links from it, but keeping the text of the links: we are treating it at this thread.

- Format the code to plain/text and separate the values using tabulation (it's a table) and save to a file: done.

- PHP will distribute the values into a new table: done.

Link to comment
Share on other sites

If <a at the start and a> at the end are constant you could do it like this.

$string1 = '<b>Some text:</b></br></br><a href="http://127.0.0.1">THIS A LINK</a></br></br><b>The End.</b>'
;~ $string2 = ReplaceIt($string1, '<a href="*anything*">THIS A LINK</a>', "THIS IS SOME UPPERCASE TEXT", 0, 0)
$sYourString = "THIS IS SOME UPPERCASE TEXT"
$sLeft = StringLeft($string1, StringInStr($string1, "<a")-1)
$sRight = StringRight($string1, StringLen($string1)-(StringInStr($string1, "a>")+1))
ConsoleWrite($sLeft & $sYourString & $sRight & @CR)

It could be if the string have only one link, but it have more than 10. If I do this it will cut off all the rest of the string.
Link to comment
Share on other sites

Like this?

$string1 = '<b>Some text:</b></br></br><a href="http://127.0.0.1">THIS A LINK</a><a href="http://127.0.0.1">and another</a></br></br><b>The End.</b>'
$sYourString = "THIS IS SOME UPPERCASE TEXT"
$string2 = StringRegExpReplace($string1, '<a href="[^"]*">.*?</a>', $sYourString)
ConsoleWrite($string2 & @CR)

There is no separator at all. Is that what you need?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Maybe you best re-start with some examples of the html and what you expect to match. If it's just getting the text from the <a></a> tags then I've posted it a dozen times and I can post it again but we need some clarification here.

What are you trying to match exactly

<a href="http:\\www.somesite.com\somepage.htm">Get this part</a>

or

<a href="http:\\www.somesite.com\somepage.htm">GET THIS PART ONLY IF IT IS UPPER CASE</a>

or

<a href="http:\\www.getthis.com\somepage.htm">Some text</a>

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Hi George,

From the example in the OP's first post, I tend to believe he wants to get rid of the hyperlink altogether. Now if that's wrong, we've some more amunition to shoot targets having "more legs" than that.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Try this.

Func _StringReplaceWildCard($sTest, $sWildCard, $sReplace)
    Local $sPattern = "(" & $sWildCard & ")"
    $sPattern = StringReplace($sPattern, ".", "\.")
    $sPattern = StringReplace($sPattern, "[", "\[")
    $sPattern = StringReplace($sPattern, "]", "\]")
    $sPattern = StringReplace($sPattern, "+", "\+")
    $sPattern = StringReplace($sPattern, "*", ".*")
    $sPattern = StringReplace($sPattern, "?", ".")
    ConsoleWrite($sPattern & @CR)
    Return StringRegExpReplace($sTest, $sPattern, $sReplace)
EndFunc


$string1 = '<b>Some text:</b></br></br><a href="[url="http://127.0.0.1"]http://127.0.0.1">THIS[/url] A LINK</a></br></br><b>The End.</b>'
 ConsoleWrite(_StringReplaceWildCard($string1, '<a href="*">THIS A LINK</a>', 'THIS IS SOME UPPERCASE TEXT') & @CR)

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

Any guess is as good as the next at this point. If you are correct then does s/he want the text left in or removed along with the link? no matter which of the sveral possibilities it's a SRE question but we need to see a clear cut question before we can provide a definitive solution. Or We could cover all the possibilties and just post a whole bunch of random SRE's. We could refer to that as the shotgun method of using RegEx, it kills everything in a wide range.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

You're right, let's stop using chainguns!

@Funkey

There is a convenient way to prevent unwanted interpretation of eventual metacharacters in the case you illustrate: make $pattern = '\Q' & $pattern & '\E' and $pattern will be interpreted verbatim. I think that letting * match .* without control is a sword with two sides.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Like this?

$string1 = '<b>Some text:</b></br></br><a href="http://127.0.0.1">THIS A LINK</a><a href="http://127.0.0.1">and another</a></br></br><b>The End.</b>'
$sYourString = "THIS IS SOME UPPERCASE TEXT"
$string2 = StringRegExpReplace($string1, '<a href="[^"]*">.*?</a>', $sYourString)
ConsoleWrite($string2 & @CR)

There is no separator at all. Is that what you need?

That's it man, thank you!

$string1 = '<b>Some text:</b></br></br><a href="http://127.0.0.1">THIS A LINK</a><a href="http://127.0.0.1">and another</a></br></br><b>The End.</b>'
$sYourString = "THIS IS SOME UPPERCASE TEXT"
$string2 = StringRegExpReplace($string1, '<a href="[^"]*">', "")
$string3 = StringReplace($string2, "</a>", "</br>", 1)
$string4 = StringReplace($string3, "</a>", "", 1)
ConsoleWrite($string4 & @CR)

Also thanking everyone who spent some minutes to answer my question.

#Solved

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