cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
john_along
Level I

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Regex Extract numbers and decimals

Regex( PIPE_LENGTH, "\D*(\d+\.?\d*)\D*", "\1" );

View solution in original post

6 REPLIES 6

Re: Regex Extract numbers and decimals

Looks [^(\d+\.\d+)] selects all but numbers, decimals and punctuation. which your are replacing with "". What is left are your numbers decimals and punctuation (). This expression is behaving the way I expect. I also tested it on your example and get the same result.

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.
john_along
Level I

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

Re: Regex Extract numbers and decimals

Regex( PIPE_LENGTH, "\D*(\d+\.?\d*)\D*", "\1" );
john_along
Level I

Re: Regex Extract numbers and decimals

Perfect. Thanks for the quick help on a stumper for me.

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???

Craige_Hales
Super User

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.

Craige