/    Sign up×
Community /Pin to ProfileBookmark

What Does This Regex Do ?

Folks,

What is this regex doing ?

[code]
$name = test_input($_POST[“name”]);
if (!preg_match(“/^[a-zA-Z-‘ ]*$/”,$name)) {
$nameErr = “Only letters and white space allowed”;
}
[/code]

I can understand it is saying you can only type alpha chars small or Capital. [a-zA-Z] but what do all the other part of the code mean ?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 25.2021 — <i>
</i>/ regex delimiter
^ starting from the very beginning of the string
[a-zA-Z-' ] match on any letter or hyphen or single quote or space
* match zero or more times that preceding character class
$ ending at the very end of the string
/ regex delimiter
Copy linkTweet thisAlerts:
@developer_webauthorFeb 26.2021 — @NogDog#1628547

>>*

>>match zero or more times that preceding >>character class


What is preceding character class ?

I guessing you mean, more than one match allowed per word. However, still got to learn what you really meant by "preceding" and "class" here. Where exactly is the 'class' symbol ? Or by 'class', you meant 'group' ? Meaning a word can have more than one instance of characters belonging to the matching characters group. Is that what you meant by "class" ? If so, still got to understand what you meant by "preceding" here or by "preceding class".
Copy linkTweet thisAlerts:
@NogDogFeb 26.2021 — * just means match 0 or more instances of whatever immediately precedes it. The expression within square brackets is called a "character class", so in this case it is the thing that immediate precedes it.

/fo*l/ would therefore match "fl", "fol", "fool", "foool", and even "fooooooooooooool".

So in the original example, it would match an empty string as well as any number of characters allowed by that character class. If you wanted it to only match if _at least one_ character was found, then you would use the + operator instead of the *, operator, as the plus means "1 - n matches", while the star means "0 - n matches".

Regular expressions are essentially their own little (and very terse) programming language. https://www.php.net/manual/en/reference.pcre.pattern.syntax.php
Copy linkTweet thisAlerts:
@developer_webauthorFeb 26.2021 — @NogDog#1628596

Thanks. Haven't checked out PHP manual link yet which you gave. Usually the manual is too hard. Got any better easier beginner friendly links or online tutorial sites ?

(I thought just flashed into my head. How about I build a .exe bot called "NogDog" ? Will be good exercise for me. Haven't been building .exe bots for nearly 4yrs now. Before that built them for 5yrs in a row. I will make NogDog an excuse to get back to it once I finish building the .PHP bot or web crawler first. What features do you prefer in a .exe web crawler or data scrapers, Not Dog ?).
Copy linkTweet thisAlerts:
@NogDogFeb 26.2021 — > @developer_web#1628606 Got any better easier beginner friendly links or online tutorial sites

Nope. I use the manual first if I know what to look for, otherwise Google until I find what I need.
Copy linkTweet thisAlerts:
@developer_webauthorFeb 27.2021 — @NogDog#1628547

Let's try rinsing you while you're in the mood and try learning as much as we can while we got the chance NogDog!

Let's begin with your code:
<i>
</i>/ regex delimiter
^ starting from the very beginning of the string
[a-zA-Z-' ] match on any letter or hyphen or single quote or space
* match zero or more times that preceding character class
$ ending at the very end of the string
/ <br/>


Q1.

^ means start from beginning of string ?

How to start from end of string and work backwards ?

Q2.

How to start from the first space in the string and work frontwards ?

Q3.

How to start from the first space on string and work backwards ?

Q4.

How to start from the last space in the string and work frontwards ?

Q5.

How to start from the last space in the string and work backwards ?

Q6-Q10

Same questions as above, only this time not how to start from first/last space and work front/backwards but how to start from first/last carriage return and work front/backwards.

Once you've shown us these 10 examples, I think a regex beginner or weakling will get stroing and gain a good grip on regex. Thanks to you.

Try answering as much as you can here. Mate.

:)
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...