Black
Black

Reputation: 20212

Match two specific words

Is it possible to find two specific words which are next to each other, with match or gmatch?

Example:

local usb_dfh   =  "Filesystem Size Used Available Use% Mounted on tmpfs"

Let's say you try to match "Mounted on". Is this possible?

Info: My goal is to find "Mounted on" and replace it with "Mounted_on".

Upvotes: 0

Views: 50

Answers (2)

Yu Hao
Yu Hao

Reputation: 122383

As the name implies, string.match and string.gmatch are used to match. To replace, use string.gsub:

usb_dfh:gsub('Mounted%s+on', 'Mounted_on')

%s+ matches one or more whitespace characters.

Upvotes: 3

Black
Black

Reputation: 20212

I found the solution for my problem.

usb_dfh = string.gsub(usb_dfh, "Mounted on", "Mounted_on")

But the question remains. Is it possible to match two specific words with gmatch?

Upvotes: 0

Related Questions