Reputation:
In the line below, what is the purpose of the '|e'? I tried looking it up but couldn't find it, and the line still does what it's supposed to when it isn't there.
pattern = /([\+|\-]?[\d]+[\.][\d|\-|e]+)[ ]+([\+|\-]?[\d]+[\.][\d|\-|e]+)[ ]+([\+|\-]?[\d]+[\.][\d|\-|e]+)/g;
EDIT:
Here is a sample of what the code is parsing.
-3.424999 -0.855454 2.257396
-1.484919 0.665606 -3.151304
1.636841 -0.848154 -0.458954
3.732041 0.187906 -1.319734
-1.756719 0.682006 0.807596
0.911641 -0.828054 3.040696
-0.218059 -0.489374 -3.806524
-1.078099 0.891706 -2.420454
Upvotes: 1
Views: 118
Reputation: 801
Normally, A |
gives alternative options, so (one|two)
matches either one or two.
However putting |
inside the []
suggests that someone doesn't understand how the []
work (they match a single instance of any character within them - or a range, so [a-z]
matches a or b or c...
I suspect, unless you have |
within the string you are matching, then you can remove all of the |
occurences from the pattern and it will still work. But it's difficult to know more without seeing some examples of the kind of string it should be matching, and what you want to capture.
(edit): Now you have provided a sample, if I was parsing that, I would use something like
/([+-]?\d+\.\d+)\s([+-]?\d+\.\d+)\s([+-]?\d+\.\d+)/
If you want to be able to accept numbers of the form 1.234e56 this would change to
/([+-]?\d+\.\d+(e[+-]?\d+)?)\s([+-]?\d+\.\d+(e[+-]?\d+)?)\s([+-]?\d+\.\d+(e[+-]?\d+)?)/
(The above assumes there will always be a decimal point)
Upvotes: 3
Reputation: 5056
[\d|\-|e]
seems weird: it's digit or a pipe or a dash or a pipe or e. It's useless to put the pipe twice.
I think this should be [\d\-e]
if the purpose was to allow digit, dash or e but not pipe.
Upvotes: 1