12.07.2015 Views

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

102 Part I ■ Learning to ProgramPattern AnchorsA pattern is quite literally a sequence <strong>of</strong> characters to be matched. Using ˆ atthe beginning <strong>of</strong> a pattern can match the beginning <strong>of</strong> a string, whereas using$ at the end <strong>of</strong> a pattern can match the end <strong>of</strong> a string. When used anywhereelse in the pattern, these strings will match their literal equivalent. Anchorscanbeusedtomakeapatternmoreexplicit.Pattern ExamplesTable 6-7 illustrates a number <strong>of</strong> common requirements <strong>for</strong> pattern matching<strong>and</strong> shows what that pattern might look like. These are general examples <strong>and</strong>mayonlyworkinspecificcases.Table 6-7: Example PatternsREQUIREMENTMatch a non-space token in a string.Match a string beginning with the text MYADDON:followed by at least one character, capturing the rest <strong>of</strong>the string.Match a number, optionally with a fractional part after adecimal point, capturing the entire number. The numbercan be positive or negative.Match an assignment in the <strong>for</strong>m xxxx=yyyy, where xxxxis alphanumeric <strong>and</strong> yyyy contains no spaces, <strong>and</strong>capture each individually.Match a single quoted string, such as 'foo' <strong>and</strong> 'bar'.Match the last nonspace token in a string.PATTERN“%S+““ ˆ MYADDON:(.+)““(%-?%d+%.?%d*)““(%w+)=(%S+)““%b’’““%S+$“Pattern Matching FunctionsLua provides four functions that accept pattern strings:string.gmatch(s, pattern)string.gsub(s, pattern, repl [, n])string.match(s, pattern [, init])string.find(s, pattern [, init [, plain]])

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!