473,320 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Extract number from string with RegExp

Claus Mygind
571 512MB
I need an example that will extract a decimal number from a string using RegExp

sample data would look something like this

"US,GS 0.0 g↵"
",GS 164.3 g↵"


and if no number is found I would want to return false
"↵"
Mar 31 '15 #1

✓ answered by Claus Mygind

Not pretty, but at least this seems to work fine

In the replace method below all characters not matching 0 to 9 or . are stripped from the msg and displayed in my output.
Expand|Select|Wrap|Line Numbers
  1. function dispWeight(msg) {
  2.     document.getElementById("curWeight").innerHTML = msg.replace( /[^.0123456789]/g,"");
  3.     }
  4.  
The RexExps is /[^.0123456789]/g
The ^ says exclude these characters
The [ ] says anything included here.
The g say repeat for the entire string

1 1528
Claus Mygind
571 512MB
Not pretty, but at least this seems to work fine

In the replace method below all characters not matching 0 to 9 or . are stripped from the msg and displayed in my output.
Expand|Select|Wrap|Line Numbers
  1. function dispWeight(msg) {
  2.     document.getElementById("curWeight").innerHTML = msg.replace( /[^.0123456789]/g,"");
  3.     }
  4.  
The RexExps is /[^.0123456789]/g
The ^ says exclude these characters
The [ ] says anything included here.
The g say repeat for the entire string
Mar 31 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: levent | last post by:
What is an elegant way (using std::stream's) to extract number of white-space separated entries in a given line of a formatted text file? e.g.: Take this section of a file, 1 2.78 4 5 -0.003...
5
by: chuck | last post by:
Hello, I am trying to figure out how to get a number (always an int) from a string. so if i have something like <input type="text" id="product13" name="product13" onblur="return...
3
by: Tracey | last post by:
I'm learning C++ and was stuck when writing code to convert a negative decimal number string to a hexdecimal number. For example, suppose the function interface is defined as: int atoi( const...
7
by: jack | last post by:
Hello, I need to get the time down the the 0.1 second in a number string. Example 09-06-03 13:45 23.4 Seconds To "0906031345234"
1
by: | last post by:
Hiya, I need to extract a string (ascii) from a buffer, my buffer is dim buf(1023) as byte This buffer containg an "array" of type xxx type xxx is defined in C as follows
1
by: nkg1234567 | last post by:
I'm trying to extract HTML from a website in the form of a string, and then I want to extract particular elements from the string using the substr function: here is some sample code that I have thus...
34
by: Umesh | last post by:
I want to extract a string abc*xyz from a text file. * indicates arbitrary no. of characters. I'm only able to do it when the string has definite no. of characters or the string length is...
3
by: ccarter45 | last post by:
I really need help with this. Your tips are greatly appreciated: import java.util.Scanner; public class PhoneNumbers { public static void main(String args){ Scanner input; ...
2
by: MaryJolly | last post by:
I want to extract number part from a string. I am doing project in VB6 and my MSFlexGrid control contains certain codes in the first column.eg. EC101,CS104,etc. I want to extract the number part from...
9
by: korea saeed | last post by:
how can i convert number string to number?(without using parsint)
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.