- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Regex Extract numbers and decimals
I have a column and I need to extract the numbers from the strings in each row. The numbers could be decimals or not.
I am using this formula: Regex( :PIPE_LENGTH, "[^(\d+\.\d+)]", "", globalreplace )
Examples:
String: Length = 3.5". Completed by John.
Expected: 3.5
Actual: 3.5..
Example 2:
Completed for CEC. Pipe depth- 4.5" (MER)
Expected: 4.5
Actual: .4.5()
Why are the extra periods and parentheses coming in? Also, I am confused by the ^ at the beginning. When I remove it I get text only and not the numbers.
Thanks
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Regex Extract numbers and decimals
Regex( PIPE_LENGTH, "\D*(\d+\.?\d*)\D*", "\1" );
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Regex Extract numbers and decimals
Would something like this work?
Regex( :PipeLength, "(\d+.\d+)", "\1" )
Not sure what the rest of your data look like but this expression is plucking out only the numbers and decimals you need. It worked on both of your examples.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Regex Extract numbers and decimals
This is better. I do have a scenario such as:
John cut 5"
This does not have decimal. Your example is returning blank.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Regex Extract numbers and decimals
Regex( PIPE_LENGTH, "\D*(\d+\.?\d*)\D*", "\1" );
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Regex Extract numbers and decimals
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Regex Extract numbers and decimals
I thought of another case: "Mark cut off .5" of his finger." I am stuck! I can't get my regex to work with this case and the others. Its one set of cases or the other!
Using look ahead...
Names Default to Here( 1 );
PIPE_LENGTH = "Length = 3.5\!". Completed by John.";
PIPE_LENGTH = "Completed for CEC. Pipe depth- 4.5\!" (MER)";
PIPE_LENGTH = "John cut 5\!"";
PIPE_LENGTH = "Mark cut .5\!" off his finger.";
Regex( PIPE_LENGTH, "\D*(?=\.)?(\d*\.?\d*)\D*", "\1" );
I wonder if using JMP Patterns would be easier.
@Craige_Hales , where are you???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Regex Extract numbers and decimals
Ouch!
Text explorer uses this pattern for numbers
([\-\+]? ?[\.,]?[0-9][\.,0-9]*( ?e ?[\-\+]? ?\d+)?)
reading it from left to right
- an optional + or -
- an optional space
- optional period or comma
- a required digit
- zero or more periods, commas, and digits
- an optional exponent (with optional bits and pieces inside)
the commas and periods are interchangeable because of different standards in various countries for how decimals and thousands separators are represented. There are some odd strings that could slip through that are not numbers, so you should write your code to expect the num() function might return a missing value.