Stumped on a string patterns example

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Stumped on a string patterns example

Geoff Smith
Hi
 
I am not great at figuring out these Lua patterns, here is a litle problem someone might be able to help me with please ?
 
I have a string that maybe in any of these forms. I will omit the outer Lua string speechmarks for clarity
 
Fred
"Fred"
'Fred'
\"Fred\"
\'Fred\'
 
I need to strip any speechmark and backslash characters and just return the string Fred as a Lua string.  The name inside the speechmarks could have spaces or numbers in it just to complicate things a bit
 
Whats the most concise Lua way of doing this ?
 
Thanks for any code snippets.
 
Regards Geoff
 
Reply | Threaded
Open this post in threaded view
|

Re: Stumped on a string patterns example

Daurnimator
On 10 October 2012 01:40, Jeff Smith <[hidden email]> wrote:

> Hi
>
> I am not great at figuring out these Lua patterns, here is a litle problem
> someone might be able to help me with please ?
>
> I have a string that maybe in any of these forms. I will omit the outer Lua
> string speechmarks for clarity
>
> Fred
> "Fred"
> 'Fred'
> \"Fred\"
> \'Fred\'
>
> I need to strip any speechmark and backslash characters and just return the
> string Fred as a Lua string.  The name inside the speechmarks could have
> spaces or numbers in it just to complicate things a bit
>
> Whats the most concise Lua way of doing this ?
>
> Thanks for any code snippets.
>
> Regards Geoff
>

First instinct should be gsub. Try:
    mystr:gsub("[\\\"']","")

Reply | Threaded
Open this post in threaded view
|

Re: Stumped on a string patterns example

Matthew Wild
In reply to this post by Geoff Smith
On 9 October 2012 15:40, Jeff Smith <[hidden email]> wrote:

> Hi
>
> I am not great at figuring out these Lua patterns, here is a litle problem
> someone might be able to help me with please ?
>
> I have a string that maybe in any of these forms. I will omit the outer Lua
> string speechmarks for clarity
>
> Fred
> "Fred"
> 'Fred'
> \"Fred\"
> \'Fred\'
>
> I need to strip any speechmark and backslash characters and just return the
> string Fred as a Lua string.  The name inside the speechmarks could have
> spaces or numbers in it just to complicate things a bit

Stripping characters can be done simply with gsub...

    local stripped = input:gsub("[\\\"']+", "")

Regards,
Matthew

Reply | Threaded
Open this post in threaded view
|

Re: Stumped on a string patterns example

Dirk Laurie-2
In reply to this post by Geoff Smith
2012/10/9 Jeff Smith <[hidden email]>:

>
> I have a string that maybe in any of these forms. I will omit the outer Lua
> string speechmarks for clarity
>
> Fred
> "Fred"
> 'Fred'
> \"Fred\"
> \'Fred\'
>
> I need to strip any speechmark and backslash characters and just return the
> string Fred as a Lua string.  The name inside the speechmarks could have
> spaces or numbers in it just to complicate things a bit
>

pat="(%p*)([%w ]*)(%p*)"
start, name, stop = str:match(pat)
if start~=stop then error "mismatched delimiters" end

This will of course also allow *Fred* etc.  Is that a problem?

Reply | Threaded
Open this post in threaded view
|

RE: Stumped on a string patterns example

Geoff Smith
Hi

Thanks for all the help guys, I went with the solution

local stripped = input:gsub("[\\\"']+", "")

I had almost come up with that myself, the mistake I made was with all the pesky escape triple blackslashes I hadnt got that quite correct

Regards Geoff
 
 
 
> Date: Tue, 9 Oct 2012 17:10:37 +0200

> From: [hidden email]
> To: [hidden email]
> Subject: Re: Stumped on a string patterns example
>
> 2012/10/9 Jeff Smith <[hidden email]>:
>
> >
> > I have a string that maybe in any of these forms. I will omit the outer Lua
> > string speechmarks for clarity
> >
> > Fred
> > "Fred"
> > 'Fred'
> > \"Fred\"
> > \'Fred\'
> >
> > I need to strip any speechmark and backslash characters and just return the
> > string Fred as a Lua string. The name inside the speechmarks could have
> > spaces or numbers in it just to complicate things a bit
> >
>
> pat="(%p*)([%w ]*)(%p*)"
> start, name, stop = str:match(pat)
> if start~=stop then error "mismatched delimiters" end
>
> This will of course also allow *Fred* etc. Is that a problem?
>
Reply | Threaded
Open this post in threaded view
|

RE: Stumped on a string patterns example

Rena

On 2012-10-12 1:05 PM, "Jeff Smith" <[hidden email]> wrote:
>
> Hi
>
> Thanks for all the help guys, I went with the solution
>
>
> local stripped = input:gsub("[\\\"']+", "")
>
> I had almost come up with that myself, the mistake I made was with all the pesky escape triple blackslashes I hadnt got that quite correct
>
> Regards Geoff
>  
>  
>  
> > Date: Tue, 9 Oct 2012 17:10:37 +0200
> > From: [hidden email]
> > To: [hidden email]
> > Subject: Re: Stumped on a string patterns example
>
> >
> > 2012/10/9 Jeff Smith <[hidden email]>:
> >
> > >
> > > I have a string that maybe in any of these forms. I will omit the outer Lua
> > > string speechmarks for clarity
> > >
> > > Fred
> > > "Fred"
> > > 'Fred'
> > > \"Fred\"
> > > \'Fred\'
> > >
> > > I need to strip any speechmark and backslash characters and just return the
> > > string Fred as a Lua string. The name inside the speechmarks could have
> > > spaces or numbers in it just to complicate things a bit
> > >
> >
> > pat="(%p*)([%w ]*)(%p*)"
> > start, name, stop = str:match(pat)
> > if start~=stop then error "mismatched delimiters" end
> >
> > This will of course also allow *Fred* etc. Is that a problem?
> >

I think backslashes aren't escaped in long strings, so sometimes it might be easier to write something like [[\"]]. But then the ugly brackets might make it just as bad... :p