How to check if the string is a regular expression or not
The only reliable check you could do is if the String
is a syntactically correct regular expression:
boolean isRegex;
try {
Pattern.compile(input);
isRegex = true;
} catch (PatternSyntaxException e) {
isRegex = false;
}
Note, however, that this will result in true
even for strings like Hello World
and I'm not a regex
, because technically they are valid regular expressions.
The only cases where this will return false
are strings that are not valid regular expressions, such as [unclosed character class
or (unclosed group
or +
.
Test if a string is regex
The only easy way to test if a regex is valid in PHP is to use it and check if a warning is thrown.
ini_set('track_errors', 'on');
$php_errormsg = '';
@preg_match('/[blah/', '');
if($php_errormsg) echo 'regex is invalid';
However, using arbitrary user input as a regex is a bad idea. There were security holes (buffer overflow => remote code execution) in the PCRE engine before and it might be possible to create specially crafted long regexes which require lots of cpu/memory to compile/execute.
Check if a String matches specific regular expression
Here's the briefest way to code the regex:
if (str.matches("(?!$)(\\d+D)?(\\d\\d?H)?(\\d\\d?M)?"))
// format is correct
This allows each part to be optional, but the negative look ahead for end-of-input at the start means there must be something there.
Note how with java you don't have to code the start (^
) and end ($
) of input, because String.matches()
must match the whole string, so start and end are implied.
However, this is just a rudimentary regex, because 99D99H99M
will pass. The regex for a valid format would be:
if (str.matches("(?!$)(\\d+D)?([0-5]?\\dH)?([0-5]?\\dM)?"))
// format is correct
This restricts the hours and minutes to 0-59
, allowing an optional leading zero for values in the range 0-9
.
How to Check if a String is a string or a RegEx?
You can't detect regular expressions with a regular expression, as regular expressions themselves are not a regular language.
However, the easiest you probably could do is trying to compile a regex from your textbox contents and when it succeeds you know that it's a regex. If it fails, you know it's not.
But this would classify ordinary strings like "foo" as a regular expression too. Depending on what you need to do, this may or may not be a problem. If it's a search string, then the results are identical for this case. In the case of "foo.bar" they would differ, though since it's a valid regex but matches different things than the string itself.
My advice, also stated in another comment, would be that you simply always enable regex search since there is exactly no difference if you split code paths here. Aside from a dubious performance benefit (which is unlikely to make any difference if there is much of a benefit at all).
Check if string matches pattern
import re
pattern = re.compile("^([A-Z][0-9]+)+$")
pattern.match(string)
Regex for string contains?
Assuming regular PCRE-style regex flavors:
If you want to check for it as a single, full word, it's \bTest\b
, with appropriate flags for case insensitivity if desired and delimiters for your programming language. \b
represents a "word boundary", that is, a point between characters where a word can be considered to start or end. For example, since spaces are used to separate words, there will be a word boundary on either side of a space.
If you want to check for it as part of the word, it's just Test
, again with appropriate flags for case insensitivity. Note that usually, dedicated "substring" methods tend to be faster in this case, because it removes the overhead of parsing the regex.
How to check if the input string is a valid Regular expression?
You can use try/catch
and the RegExp
constructor:
var isValid = true;
try {
new RegExp("the_regex_to_test_goes_here");
} catch(e) {
isValid = false;
}
if(!isValid) alert("Invalid regular expression");
Check whether a string matches a regex in JS
Use regex.test()
if all you want is a boolean result:
console.log(/^([a-z0-9]{5,})$/.test('abc1')); // false
console.log(/^([a-z0-9]{5,})$/.test('abc12')); // true
console.log(/^([a-z0-9]{5,})$/.test('abc123')); // true
Check if a string matches a regex in Bash script
You can use the test construct, [[ ]]
, along with the regular expression match operator, =~
, to check if a string matches a regex pattern (documentation).
For your specific case, you can write:
[[ "$date" =~ ^[0-9]{8}$ ]] && echo "yes"
Or more a accurate test:
[[ "$date" =~ ^[0-9]{4}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])$ ]] && echo "yes"
# |\______/\______*______/\______*__________*______/|
# | | | | |
# | | | | |
# | --year-- --month-- --day-- |
# | either 01...09 either 01..09 |
# start of line or 10,11,12 or 10..29 |
# or 30, 31 |
# end of line
That is, you can define a regex in Bash matching the format you want. This way you can do:
[[ "$date" =~ ^regex$ ]] && echo "matched" || echo "did not match"
where commands after &&
are executed if the test is successful, and commands after ||
are executed if the test is unsuccessful.
Note this is based on the solution by Aleks-Daniel Jakimenko in User input date format verification in bash.
In other shells you can use grep. If your shell is POSIX compliant, do
(echo "$date" | grep -Eq ^regex$) && echo "matched" || echo "did not match"
In fish, which is not POSIX-compliant, you can do
echo "$date" | grep -Eq "^regex\$"; and echo "matched"; or echo "did not match"
Caveat: These portable grep solutions are not water-proof! For example, they can be tricked by input parameters that contain newlines. The first mentioned bash-specific regex check does not have this issue.
Test if characters are in a string
Use the grepl
function
grepl( needle, haystack, fixed = TRUE)
like so:
grepl(value, chars, fixed = TRUE)
# TRUE
Use ?grepl
to find out more.
Related Topics
PHP Readfile() Adding Extra Bytes to Downloaded File
Switching to Prepared Statements
Quoting Constants in PHP: "This Is a My_Constant"
How to Stop Curl from Using 100 Continue
Number_Format() Causes Error "A Non Well Formed Numeric Value Encountered"
Phpseclib - How to Connect Using Username, Key and Password (Not a Key Passphrase)
Do I Need to Escape My Variables If I Use MySQLi Prepared Statements
How to Convert an Iso8601 Date to Another Format in PHP
How to Insert Multiple Rows in PHP Pdo MySQL
Getimagesize() Not Returning False When It Should
Can't Connect to Postgresql with PHP Pg_Connect()
Comparing Floats - Same Number, But Does Not Equal
Using PHP Code in Smarty Tpl File
PHP Session Id Changing on Every Request
Set Httponly and Secure on PHPsessid Cookie in PHP