This question already has answers here:
Split String By Multiple Spaces NodeJS
(2 answers)
Closed 5 years ago.
The output from ps can give you lines like this one:
0.0 0.2 88 /usr/sbin/securityd
Or like this one:
47.0 0.3 7770 node
Or even:
1.0 2.5 585 /Applications/PhpStorm.app/Contents/MacOS/phpstorm
The number of white spaces between columns is variable according to the content of the columns. If you want to extract the columns values I did an ugly solution:
line.replace(' ', ' ').replace(' ', ' ').replace(' ', ' ').split(' ');
That covers everything from 4 spaces to 2. If I want to support 5 spaces I'll need another replace, and to be honest I don't like this solution. I thought about parsing each char at the time looking for spaces and accumulating consecutive nonspaces chars, but I wonder if there is a better way, maybe with regular expressions?
You can use regular expressions with the split function to match any number of spaces.
var lines = line.split(/\s+/);
The \s+ regular expression matches one or more spaces.
A runnable example with the strings you provided:
var lines = [
"0.0 0.2 88 /usr/sbin/securityd",
"47.0 0.3 7770 node",
"1.0 2.5 585 /Applications/PhpStorm.app/Contents/MacOS/phpstorm"
];
for (var i in lines)
console.log(lines[i].split(/\s+/));
Related
This question already has answers here:
how to extract floating numbers from strings in javascript
(3 answers)
Closed 2 years ago.
Lately iv'e been trying to find some ways to manipulate a string (for some project of mine) and i'm having a hard finding something that will mach my case.
usually the string will include 3 numbers (can also be decimal - that's what make it more complicated) and separated by 1 / 2 signs ("-", "x", "*" and so on...)
i did some research online and found this solution (which i thought it was good)
.match(/\d+/g)
when i tried it on some case the result was good
var word = "9-6x3"
word = word.match(/\d+/g)
it gave me array with 3 indexes, each index held a number ['9', '6', '3'] (which is good), but if the string had a dot (decimal number) this regex would have ignored it.
i need some regex which can ignore the dots in a string but can achieve the same result.
case =
var word = "9.5-9.3x7" output = ['9.5', '9.3', '7']
Try this regular expression to allow for an optional decimal place:
word.match(/\d+([\.]\d+)?/g)
This says:
\d+ - any number of digits
([\.]\d+)? - optionally one decimal point followed by digits
Here is a simple regex that suits your requirement,
/\d+\.?\d*/g
This question already has answers here:
split string into array of n words per index
(5 answers)
Closed 6 years ago.
I have the following string:
The water content is considered acceptable for this voltage class. Dielectric Breakdown Voltage is unacceptable for transformers > 288 KV. Power factors, Interfacial Tension and Neutralization Number are acceptable for continued use in-service.".
I want to split the string into lines so that every line will contain at max 5 words in each line.
I want to control the number of words in each line dynamically, so that tomorrow I will be able to split the string into lines where each line contain at max N sentences in each line.
var string="The water content is considered acceptable for this voltage class. Dielectric Breakdown Voltage is unacceptable for transformers > 288 KV. Power factors, Interfacial Tension and Neutralization Number are acceptable for continued use in-service.";
var yourSplit=function(N,string){
var app=string.split(' '),
arrayApp=[],
stringApp="";
app.forEach(function(sentence,index){
stringApp+=sentence+' ';
if((index+1)%N===0){
arrayApp.push(stringApp);
stringApp='';
}else if(app.length===index+1 && stringApp!==''){
arrayApp.push(stringApp);
stringApp='';
}
});
return arrayApp;
};
console.log(yourSplit(5,string));
console.log(yourSplit(3,string));
console.log(yourSplit(8,string));
Could any of you help me with a regular expression which will accept these:
For every thousand a comma will be accepted.
No less than 4 decimal points in number
I've been testing this pattern but fails in some scenarios:
^\d+(\,\d+{1.3} \.d{1.4})*$
Valid inputs:
1
11
111
1,111
1,111,111
1.1111
11.1111
111.1111
1,111.1111
Invalid inputs:
Any letter
1,1
1.1
1.11
1,11.11
1,111.1
The main criterions are:
(4 decimal points are obligatory).
(3 numbers per thousand must have a comma.)
Is this more complicated than
/^\d\d?\d?(,\d\d\d)*(\.\d\d\d\d)?$/
or if you prefer
/^\d{1,3}(,\d{3})*(\.\d{4})?$/
I've got following formats, that are acceptable
1200000,00
1200000.00
1,200,000.00
1 200 000.00
1 200 000,00
1 200 000,0000
-1 200 000.00
At the moment I was able to verify only ^-?\\d+$, ^-?\\d+[\\,\\.]\\d{2}$, ^-?\\d+[\\,\\.]\\d{2,}$. Two last format are separate, so that I would know is rounding needed or not. All three format use gm flags to check string from start ^ to end $.
Those regular expressions cover only first two elements in list. Other elements, that use commas and spaces for thousand separation are not verified yet and I'm not sure how to achieve that.
Also there is a "beautifier" expression (\\d)(?=(\\d{3})+(?!\\d)), that will take this 1200000,00 and turn it into 1 200 000,00 with such usage '1200000,00'.replace(('(\\d)(?=(\\d{3})+(?!\\d))', 'g'), '$1 ').
So question states, what would be a correct regular expression to validate such format 1 200 000.00 or 1,200,000.00? Since I assume difference with \s\, could be easily done in same expression.
Thank you.
For validating the last two numbers, you can use the following:
^-?\d{1,3}(?:[\s,]\d{3})*(?:\.\d+)?$
1 2 3 4 5
Optional minus sign
1..3 digits
Zero or more fragments that consist of
comma or space
3 digits
optional fraction part consisting of a dot followed by 1 or more digits.
This doesn't directly solve the problem due to me misreading. But it might still be useful to someone so I'll let it stay.
Stop trying to solve every problem with regex. Regex is great when you have one or two very well defined strings. Not a million formats.
This can be solved with minimal regex. Magic is in the bold part.
var numbers = [
"1200000,00",
"1200000.00",
"1,200,000.00",
"1 200 000.00",
"1 200 000,00",
"1 200 000,0000",
"-1 200 000.00"
];
var parseWeirdNumber = function(numberString) {
//Split numbers to parts. , . and space are all valid delimiters.
var numberParts = numberString.split(/[.,\s]/);
//Remove the last part. **This means that all input must have fraction!!**
var fraction = numberParts.pop();
//Rejoin back without delimiters, and reapply the fraction.
//parseFloat to convert to a number
var number = parseFloat(numberParts.join('') + "." + fraction);
return number;
}
numbers = numbers.map(parseWeirdNumber);
console.log(numbers);
This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 2 years ago.
Let's say I have an input field and want to parse all of the numbers from the submitted string. For example, it could be:
Hi I'm 12 years old.
How do I parse all of the numbers without having a common pattern to work with?
I tried:
x.match(/\d+/)
but it only grabs the 12 and won't go past the next space, which is problematic if the user inputs more numbers with spaces in-between them.
Add the g flag to return all matches in an array:
var matches = x.match(/\d+/g)
However, this may not catch numbers with seperators, like 1,000 or 0.123
You may want to update your regex to:
x.match(/[0-9 , \.]+/g)
var words = sentence.split(" ");
var numbers = words.filter(function(w) {
return w.match(/\d+/);
})