Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- JMP User Community
- :
- Discussions
- :
- Regex Extract numbers and decimals

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

May 14, 2020 11:01 AM
(762 views)

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

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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

Learn it once, use it forever!

6 REPLIES 6

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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.

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.

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- 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

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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

Learn it once, use it forever!

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Regex Extract numbers and decimals

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

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Regex Extract numbers and decimals

Created:
May 14, 2020 1:28 PM
| Last Modified: May 14, 2020 1:31 PM
(729 views)
| Posted in reply to message from john_along 05-14-2020

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

Learn it once, use it forever!

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- 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.

Craige

Article Labels

There are no labels assigned to this post.