2

this is a string

"'"id"':001 (it is visualized "id":001)

I want to capture only the values in lua. if there is not double quotes, i can extract values only. (use something:gmatch((%a+)%sd:%s(%d+)))

Is there anyone who solve this problem?

Jorge Campos
  • 20,662
  • 7
  • 51
  • 77
darren
  • 103
  • 7

1 Answers1

2

You may use a "(%w+)"%s*:%s*(%d+) pattern:

local example = [[ "id":001 "id2":002 ]]
for i,y in example:gmatch([["(%w+)"%s*:%s*(%d+)]]) do
  print(i, y)
end

See the Lua demo, output:

id  001
id2 002

The "(%w+)"%s*:%s*(%d+) pattern matches

  • " - a double quote
  • (%w+) - Group 1: one or more alphanumeric chars (use [%w_]+ to also match _)
  • " - a "
  • %s*:%s* - a colon enclosed with 0+ whitespaces
  • (%d+) - Group 2: one or more digits
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • I have one more question. I want to extract the values in each. Is there something to do that i can? – darren Nov 24 '17 at 13:12
  • @EuDdeumPark If you have any doubts, please post a link to the Lua fiddle if you still have trouble with it. I actually do not understand what you meant by "extract the values in each". `string.gmatch` extracts all matches. If you do need a specific match, use `string.match` with a pattern like `[["id"%s*:%s*(%d+)]]`. – Wiktor Stribiżew Nov 26 '17 at 10:27
  • @Stribizew I used for loop, and I solved my newest problem. Thank you. – darren Nov 26 '17 at 11:28
  • @EuDdeumPark: Ok, glad it worked for you. Please also consider upvoting if my answer proved helpful to you (see [How to upvote on Stack Overflow?](http://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow)). – Wiktor Stribiżew Nov 26 '17 at 14:55