473,322 Members | 1,540 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,322 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 1529
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.