Matching Floating Point Numbers with a Regular Expression Matching Floating Point Numbers with a Regular Expression (e?\d )? this is an optional scientific notation (\d*)? optionally we can have a dot character an zero or more digits after it (e?\d )? this is an optional (0 or 1 matches due to ending '?') scientific notation first character is dot (inside brackets or else it is a wildcard character) \d (e?\d )? this is for numbers starting with '.' \d (\d*)?(e?\d )? This is for numbers starting with a digit ( since we have 2 sub expressions we need to put them in parenthesis ? /- sing is optional since ? means 0 or 1 matches In order to support both '1.' and '.1' we need an OR operator ('|') in order to make sure we exclude '.' from matching. '.1e3' is valid, but '.e3' and 'e3' are invalid '-1.' and '-.1' are valid but '.' and '-.' are invalid Preceding sign of number ('-' or ' ') is optional I want to match what most languages consider valid numbers (integer and floats): The long regex that this class contains caters for various possibilities that the OP probably didn't have in mind, but ignoring for simplicity the parts of it that deal with NaN, infinity, Hexadecimal notation and exponents, and using \d rather than the POSIX notation for a single digit, I can reduce the important parts of the regex for a signed floating point number with no exponent to: This seems tricky at first, but one way of finding inspiration is to look at the OpenJDK source for the (String) method (start at, click "browse", navigate down /src/share/classes/java/lang/ and find the Double class). the following are not allowed:Ī signed decimal point with no digits (i.e. 5678)Īt the same time, you must ensure that there is at least one digit somewhere, i.e. , 1234.)ĭigits after the decimal point only (e.g.0. 0.35, 22.165)ĭigits before the decimal point only (e.g. The complication is that you have to match all of the following possibilities:ĭigits both before and after the decimal point (e.g. I don't think that any of the answers on this page at the time of writing are correct (also many other suggestions elsewhere on SO are wrong too). This is typically not the default, but be on the lookout for these flags. Also keep in mind that some engines allow you to set flags which change the definition of ^ and $, matching the beginning/end of a line rather than the beginning/end of the entire input. Note that some regex engines have a validate, isMatch or similar function, which essentially does what I've described automatically, returning true if a match is found and false if no match is found. So, given the input 1.2, a match will be found, but given apple 1.2 pear no matches will be found. This will only find a match if the entire input is a floating point number, and will not find a match if the input contains additional characters. Java, for example, uses \ as an escape character within it's strings, so if you want to place a literal backslash character within a string, you must escape it: This is because you are basically coding in a language within a language. For those languages that don't, escaping can be a problem. Some languages have built-in support for regexes, such as JavaScript. Most likely, there's an issue with escaping. That said, most regex engines (actually, all of them, as far as I know) would accept \. See Is a Regex the Same as a Regular Expression? at Re圎gg.com for more information.) (Incidentally, to help reduce confusion, many have taken to using "regex" or "regexp" to describe these enhanced matching languages. For this reason, a lot depends on what language, API, framework or library you are using. So, while most regular expression engines resemble one another, there is actually no standard. However, software engineers have added many extensions that take these implementations far beyond the formal definition. "Regular expressions", as implemented in most modern languages, APIs, frameworks, libraries, etc., are based on a concept developed in formal language theory.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |