Need help with understanding String patterns

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to learn how to use string patterns

  2. What is the issue? I don’t understand how to use string patterns even after looking at the developer hub

  3. What solutions have you tried so far? I have tried looking at this String Patterns but it’s still confusing i have also tried looking for any past help on the dev forum and found nothing.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Hello i need help with understanding string patterns i tried the developer hub but i still can’t understand how to do string formats any help will be appreciated :slight_smile:

when i say string patterns i’m talking about these

%a
%l
%u
%d
%p
%w
%s
%c
%x
%z

I will try to explain the best I can :

%a
lets say you have this string : abcd123
and you wanted to only print this without the numbers.
One of the ways to do that, is to use %a which will check only for letters.
Example:

local stg = "abc123"

print(stg:match("%a+")) -- would print abc
--the "+" sign will just make sure it will print every letter in the string

%l
for lower case letters. Will only get the lower case letters .example:

local stg = "Abc"

print(stg:match("%l+")) -- would print bc
--the "+" sign will make sure to print every lower case letter in the string

%u
for upper case letters, will only get the upper case letters. Example:

local stg = "ADdye"

print(stg:match("%u+")) -- would print AD

%d
will check for numbers. Example:

local stg = "abc123"

print(stg:match("%d+")) -- would print 123

%p
Punctuations. Example:

local stg = "hi!?"

print(stg:match("%p+")) -- would print !?
1 Like

A string pattern is a combination of characters that can be used to find very specific pieces,

Quoted from the String Patterns article, but string patterns is basically what you use in order to find certain words or phrases inside of another string. Think of it as finding a specific word in an existing string.

The ones that you want to know are actually symbol combinations for more specialized functions. If you scroll down, there’s already a library of what the functions mean but here’s a screenshot.

Let’s use "Hello World!" as an example string.

Say like you want to find the only the lowercase letters, you’d use %l to find those lowercase letters.

local string = "Hello World!"

print(string:match("%l+")) -- this will only print the available lowercase letters in "Hello World!" (elloorld)

If you just want the uppercases of a string you can do this instead:

local string = "Hello World!"

print(string:match("%u+")) -- this will only print the available uppercase letters in "Hello World!" (HW)

We use the + next to it so that it’ll get all of the available variables inside of a string. These operators within the symbol strings is called modifiers.

It’s also on the page, but here’s an image of the available functions:

Like I said, it’s better for you to experiment it on your own for your best understanding. Though, I hope this helped you understand! If it didn’t you got DevForum.

3 Likes