what the regular expression of a line of string containing ONLY float numbers separated with spaces or tabs. The float number can be negative, like -999.999
views:
35answers:
3
+2
A:
(?:-?(?:\d+(?:\.\d*)|.\d+)[ \t]*)+
is one possibility. In more readable format:
(?:
-? # Optional negative sign
(?:
\d+(?:\.\d*) # Either an integer part with optional decimal part
|
.\d+ # Or a decimal part that starts with a period
)
[ \t]* # Followed by any number of tabs or spaces
)+ # One or more times
Amber
2010-07-01 01:58:59
@Amber, what's the (?: for?
Paul
2010-07-01 02:09:06
@Paul: `(?:` signifies a *non-capturing* group. Normally when you wrap something in parentheses, the regex engine keeps track of what matches that stuff, so you can access it once the regex has finished. This is really good for things like extracting data, but is a bit less efficient than if the engine didn't have to keep track of it. You can use noncapturing groups to speed things up slightly, at the expensive of making your regex look even more chicken-scratchy.
Anon.
2010-07-01 02:26:47
Non-capturing groups are also handy if you *do* want to capture extract certain data, but also need to group some things that you aren't capturing - it keeps the list of captured groups "clean".
Amber
2010-07-01 02:40:49
+1
A:
A regex for a float would look like this: -?\d+\.?\d+
A whitespace separator looks like this: \s
Put them together, allow it to repeat, make sure the end has a float (not a separator):
((-?\d+\.?\d*)\s)*(-?\d+\.?\d*))
The escaping and \d
vs [0-9]
might change, depending on your flavor of regex.
Stephen
2010-07-01 02:00:00
+1
A:
Let's come up with a regex for a float, and then see what we can do about the rest.
A float is:
- An optional negative sign
- Followed by a number of digits
- Followed by an optional decimal point and then more digits
- Followed be "e"
- Followed by a number of digits (with an optional sign).
Put that together, and we get:
/-?[0-9]+(\.[0-9]+)?([Ee][+-]?[0-9]+)?/
Now, this is pretty loose, but you can tweak it if you want to tighten it up a little. Now, for any number of these with spaces in between, it's pretty trivial:
/^(F\s+)+$/
Put it all together, we end up with:
/^(-?[0-9]+(\.[0-9]+)?([Ee][+-]?[0-9]+)?\s+)+$/
Anon.
2010-07-01 02:00:19
You can generally use `\d` instead of `[0-9]`. Also, what's the `F` doing in your second subregex?
Amber
2010-07-01 02:03:39
@Amber: I'm using that as a standin for the float regex, to better show the structure of the resulting solution. @Paul: The anchors at the start and end of the regex ensure that the *only* content on the line is stuff that matches the regex.
Anon.
2010-07-01 02:18:48
@Paul : The regex only allows for digits (unless it's an e inside digits), '-', '.' and whitespace, so it _can't_ have strings.
Stephen
2010-07-01 02:19:40