Question marks in regular expressions

The key difference between ? and ?? concerns their laziness. ?? is lazy, ? is not. Let’s say you want to search for the word “car” in a body of text, but you don’t want to be restricted to just the singular “car”; you also want to match against the plural “cars”. Here’s an example sentence: I own three cars. Now, if I wanted … Read more

How to test valid UUID/GUID?

Currently, UUID’s are as specified in RFC4122. An often neglected edge case is the NIL UUID, noted here. The following regex takes this into account and will return a match for a NIL UUID. See below for a UUID which only accepts non-NIL UUIDs. Both of these solutions are for versions 1 to 5 (see the … Read more

Escaping Discord subset of markdown

I’m trying to escape the subset of markdown that Discord supports (*, _, `, ~). Characters that are already escaped should not have additional backslashes added. This is what I have:  Run code snippetExpand snippet This works fine, minus the fact that multiple markdown characters against each other will not all be escaped. I’m not sure how to … Read more

Regex to check for new line

I want to check if an if statement is on one line or the next line without a brace like so: or: The regex i have currently is But doesnt work. Anyone have any ideas? To elaborate the only If statement that would not be pulled by this regex is the following:

Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters

Minimum eight characters, at least one letter and one number: Minimum eight characters, at least one letter, one number and one special character: Minimum eight characters, at least one uppercase letter, one lowercase letter and one number: Minimum eight characters, at least one uppercase letter, one lowercase letter, one number and one special character: Minimum … Read more

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 … Read more

Regex optional group

You can easily simplify your regex to be this: I’m not sure whether the input string without the first group will have the underscore or not, but you can use the above regex if it’s the whole string. regex101 demo As you can see, the matched group 1 in the second match is empty and … Read more

Using the star sign in grep

The asterisk is just a repetition operator, but you need to tell it what you repeat. /*abc*/ matches a string containing ab and zero or more c’s (because the second * is on the c; the first is meaningless because there’s nothing for it to repeat). If you want to match anything, you need to say .* — the dot … Read more

Regex that accepts only numbers (0-9) and NO characters

Your regex ^[0-9] matches anything beginning with a digit, including strings like “1A”. To avoid a partial match, append a $ to the end: This accepts any number of digits, including none. To accept one or more digits, change the * to +. To accept exactly one digit, just remove the *. UPDATE: You mixed up the arguments to IsMatch. The pattern should be the second argument, … Read more

Regular expression for exact match of a string

if you have a the input password in a variable and you want to match exactly 123456 then anchors will help you: in perl the test for matching the password would be something like EDIT: bart kiers is right tho, why don’t you use a strcmp() for this? every language has it in its own way … Read more