In regex, what does [\w*] mean?

Quick answer: ^[\w*]$ will match a string consisting of a single character, where that character is alphanumeric (letters, numbers) an underscore (_) or an asterisk (*). Details: The “\w” means “any word character” which usually means alphanumeric (letters, numbers, regardless of case) plus underscore (_) The “^” “anchors” to the beginning of a string, and … Read more

Get group names in java regex

There is no API in Java to obtain the names of the named capturing groups. I think this is a missing feature. The easy way out is to pick out candidate named capturing groups from the pattern, then try to access the named group from the match. In other words, you don’t know the exact … Read more

Regular Expression to MATCH ALL words in a query, in any order

You can achieve this will lookahead assertions ^(?=.*\bmeat\b)(?=.*\bpasta\b)(?=.*\bdinner\b).+ See it here on Regexr (?=.*\bmeat\b) is a positive lookahead assertion, that ensures that \bmeat\b is somewhere in the string. Same for the other keywords and the .+ is then actually matching the whole string, but only if the assertions are true. But it will match also … Read more

How to use regex with optional characters in python?

You can put a ? after a group of characters to make it optional. You want a dot followed by any number of digits \.\d+, grouped together (\.\d+), optionally (\.\d+)?. Stick that in your pattern: import re print re.match(“(\d+(\.\d+)?)”, “3434.35353”).group(1) 3434.35353 print re.match(“(\d+(\.\d+)?)”, “3434”).group(1) 3434

Regex to match an IP address [closed]

Don’t use a regex when you don’t need to 🙂 $valid = filter_var($string, FILTER_VALIDATE_IP); Though if you really do want a regex… $valid = preg_match(‘/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/’, $string); The regex however will only validate the format, the max for any octet is the max for an unsigned byte, or 255. This is why IPv6 is necessary – … Read more