I believe I see where OP is coming from, remarking on matching. I think it’s quite understandable to wonder how printf
and a regexp are similar or different. The answers given already are correct, but I hope to provide a more basic overview.
It might help to distinguish between substitution patterns and rexexp. A substitution pattern is a way to communicate a context switch. Your example
@printf("%s\nThis is a float: %f\nThis is an integer: %d", "This is a string", 4, 9.4)
has a string that should mostly be printed literally, but also has some tokens like %s
, %f
, %d
that mean “don’t print this literally, but instead substitute each token with the arguments following this string,” which should be a string, a float, and an integer. The tokens represent a switch from literal text to something else.
A regexp has some similar features, in that there are tokens that switch context to something else. The shell command ls *.txt
or dir *.txt
says, list all the file (or directory) names that end with literal .txt
, but start with anything. The *
means match anything from the list produced by ls
. There is a whole language of fancy things that could be matched, as indicated by @Henrique_Becker.
The question of scripting vs. “programming” language is interesting. There is no hard distinction to be made, but one could over-generalize that regexps are somewhat more applicable to scripting, in that scripts are often written quickly and casually, and are often meant to interact with files and such where there may be a pattern to their names. Many a scripter will quickly whip up a regexp to get something done.
In “real programming,” one might be more cautious with regexps, because there is a lot of opportunity for things to go horribly wrong, and create security holes and such. One would either avoid regexp, and/or test them carefully. But I am sure there are many examples counter to this generalization, plus no definition of real vs. casual programming.