Pattern matching help.

Regular expressions have never been something I would call a strength and Lua's pattern matching is even harder for me to figure it out (though it's probably simpler).

Anyway, I have a string of text that may contain an anchor tag and URL:

blah blah blah <a href="http://www.mysite.com">Some Text</a> blah blah blah.

I want to remove the link, leaving the text.

Here is the code I'm trying to use:

1
storyBody = story.content_encoded:gsub("<a%w+>(%w+)<%/a>", "%1")

Wow, must be really some censored content, it has an extra x after xxx ;)

On the serious side,
Why are you using gsub? gsub is for substitution or replacing portions, and it requires a pattern and the replacement, however what you need is gmatch to obtain the string. Just wait for my next update, I'll give you something to work with.

cheers,

?:)

Yea, its some seriously erotic text :-P

I'm using gsub because I want to remove the link, but leave the text that the link wraps, in other words:

substitute:

<a href="http://mysite.com">Click Here</a>

with

Click Here

I suppose I could do that with .find and then some substring operations, but substitute seems like the route to go.

Try this,

1
2
3
4
stringy = "<a href='http://www.oz-apps.com/page/1.html'> test </a>"
for k, v in string.gmatch(stringy, "%S*") do
    print(k)
end

This regex will match anything between > and <

1
>(.*?)<

Remember that this site is your best friend when testing out regex expressions:

http://myregexp.com/signedJar.html

Actually this will probably work for me:

1
2
3
stringy = "blah blah blah <a href='http://www.oz-apps.com/page/1.html'>test</a> blah blah blah "
stringz = stringy:gsub("<a[%w%s%p]+>([%w%s%p]+)</a>", "%1")
print(stringz)
views:1633 update:2011/10/5 8:48:05
corona forums © 2003-2011