Regex is so powerful to search any pattern you want, and it can used in almost every language.
However, considering the performance, try to use the simple replace / search if not necessary.
e.g. in php, we will use str_replace
to replace the simple string rather than using preg_replace
.
In the folliwng snippet, we can see most of regex syntax is quite consistent across different languages. Only the delimiter is not the same.
Use in Shell
# \ back slash is the escape character.
# () parentheses is the sub expression
# [] square bracket match any character inside
# ^ caret inside square bracket mean except, but outside mean begin with
ag '\$conf\[(.+)\]' ./**/*.php
Use in JS
In JS, we can use regex with forward slash as the literal notation.
pattern = \(.*)Key Word(.*)\;
Use in PHP
// / forward slash is the delimiter here.
// \ back slash is the escape character
// and quote is needed to wrap all the things
$pattern = '/(\s*)Key Word(.*)/';
// preg_math will return 1 when found, return 0 when not found,
// return false when error happen
if (preg_match($pattern, $hay)) {
dd('found');
}