69882

Lua: %b with modifiers

Question:

I'm trying to match a string that contains Lua code.

a = [[workspace.Object["Child"]:remove()]]

to do this, I'm trying to create an option where either .x or ['x'] would be matched regardless of what order they are in or how many of them there are.

A couple problems I have run into:

<ul><li>How do I match more than one combined character/pattern between brackets? [abc] matches a or b or c, but not abc.</li> <li>How do I add a modifier to %b[]? ex. %b[]+ to match ['x']['x']['x']</li> <li>If I could match something like %[.-%] *, that would work the same.</li> </ul>

Answer1:

Lua does not fully support regexps.<br /> But you can do your task step-by-step, using intermediate strings.

local str0 = [[workspace.Object["Child"]['xx'][5].xxx:remove()]] local str = str0 :gsub('%b[]', function(s) return s:gsub('^%[%s*([\'"]?)(.*)%1%s*%]$','{%2}') end ) :gsub('[%.:]%s*([%w_]+)','{%1}') print(str0) print(str) print() for w in str:gmatch'{(.-)}' do print(w) end --------------------------- -- output --------------------------- workspace.Object["Child"]['xx'][5].xxx:remove() workspace{Object}{Child}{xx}{5}{xxx}{remove}() Object Child xx 5 xxx remove <hr />

<strong>EDIT :</strong>

local str0 = [[workspace.Object["Child"]['xx'][5][ [=[xxx]=] ]:remove()]] local str = str0 :gsub('%b[]', function(s) return s:gsub('^%[%s*([\'"]?).*%1%s*%]$','{%0}') end ) :gsub('%.%s*[%w_]+','{%0}') :gsub(':%s*[%w_]+%s*([\'"]).-%1','{%0}') :gsub(':%s*[%w_]+%s*%b()','{%0}') :gsub('{(:%s*remove%s*%(%s*%))}','%1') :gsub('}%s*{', '') :gsub('([%w_]+)%s*(%b{})%s*:%s*remove%s*%(%s*%)', function(s1, s2) return 'removefilter('..s1..s2:match'^{(.*)}$'..')' end ) :gsub('([%w_]+)%s*:%s*remove%s*%(%s*%)','removefilter(%1)') :gsub('[{}]', '') print(str0) print(str) --------------------------- -- output --------------------------- workspace.Object["Child"]['xx'][5][ [=[xxx]=] ]:remove() removefilter(workspace.Object["Child"]['xx'][5][ [=[xxx]=] ])

Recommend

  • Combining two double[] arrays into double[,]
  • Webpack use UglifyJSPlugin to ONLY remove comments
  • Why does non-const method hide const overload?
  • Pattern combining type test and literal
  • Pipelining between two SEPARATE Powershell processes
  • Matching multiple strings to one long string using patterns
  • How do I split this string into an array?
  • IIS 7.5 404 Error for .PDF files
  • Scala split a multi line string by lines that contain all hyphens
  • how to load css classes from my own project specfic css in Sitecore's RAD editor?
  • Autohotkey script running program with command line arguments
  • Does “internal” visibility modifier in Kotlin work yet?
  • How does inheritance and polymorphism work in this situation?
  • Using HTML/CSS for UI in XNA?
  • C function strchr - How to calculate the position of the character?
  • Use sed with regex and (
  • What Makes These Two Array Adds Different?
  • Hide HTML elements without javascript, only CSS
  • Trying to get the char code of ENTER key
  • Django model inheritance, filtering models
  • Why use database factory in asp.net mvc?
  • How can I set a binding to a Combox in a UserControl?
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • How to have background script and something similar to a default popup?
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Regex thinks I'm nesting, but I'm not
  • Problems to linebreak with an int in JLabel
  • WinForms: two way TextBox problem
  • R: gsub and capture
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Revoking OAuth Access Token Results in 404 Not Found
  • Angular 2 constructor injection vs direct access
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Android Google Maps API OnLocationChanged only called once
  • Django query for large number of relationships
  • UserPrincipal.Current returns apppool on IIS