# Extract numbers from Text

**URL:** <https://discourse.mcneel.com/t/extract-numbers-from-text/99154>\
**Category:** Scripting\
**Tags:** ghpython, grasshopperrhino\
**Created:** [March 31, 2020, 1:40am UTC](https://discourse.mcneel.com/t/extract-numbers-from-text/99154 "2020-03-31T01:40:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [March 31, 2020, 1:40am UTC](https://discourse.mcneel.com/t/extract-numbers-from-text/99154/1 "2020-03-31T01:40:19Z")

</div>

Hello  
To extract numbers from this list i use regular components **Text split** and **Replace text** or **Wombat Replace text multiple**.  
Is there an easiest way with Python or Grasshopper ?

[text.gh](https://discourse.mcneel.com/uploads/short-url/ahYUI7POTMeaaM9DzHdaeeryKkT.gh) (12.4 KB)

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/9/6/9685a0b358ea7bd98276bc6efaff621f644a6e33.png)

---

<div class="post-metadata">

**Author:** ![Mahdiyar](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mahdiyar/32/472862_2.png) [@Mahdiyar](https://discourse.mcneel.com/u/Mahdiyar)\
**Post date:** [March 31, 2020, 3:52am UTC](https://discourse.mcneel.com/t/extract-numbers-from-text/99154/2 "2020-03-31T03:52:42Z")

</div>

Not necessarily easier, but more parametric approach:

 ![ExtractNumber](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/9/d/9dff6a846a3f8f90afef6a816a7cdafed0f60dab.png)  
[ExtractNumber.gh](https://discourse.mcneel.com/uploads/short-url/a5HgFnA1gQkTfwju3ntmYEZx5Vc.gh) (15.9 KB)

---

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [March 31, 2020, 4:44am UTC](https://discourse.mcneel.com/t/extract-numbers-from-text/99154/3 "2020-03-31T04:44:10Z")

</div>

Thank you; i also tried with Python, i know there are more simple ways

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/6/3/6396abc5538d8f76e623f1a5c3b7b117eb5007e9.png)

---

<div class="post-metadata">

**Author:** ![Mahdiyar](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mahdiyar/32/472862_2.png) [@Mahdiyar](https://discourse.mcneel.com/u/Mahdiyar)\
**Post date:** [March 31, 2020, 5:15am UTC](https://discourse.mcneel.com/t/extract-numbers-from-text/99154/4 "2020-03-31T05:15:33Z")

</div>

```
import re 
a = re.findall(r'[0-9.]+', x)

```

[ExtractNumber.gh](https://discourse.mcneel.com/uploads/short-url/qjAcnbWQcVnQP4tgHHy2jlqFOyg.gh) (15.7 KB)

---

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [March 31, 2020, 5:17am UTC](https://discourse.mcneel.com/t/extract-numbers-from-text/99154/5 "2020-03-31T05:17:10Z")

</div>

Thank you

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [March 31, 2020, 8:00am UTC](https://discourse.mcneel.com/t/extract-numbers-from-text/99154/6 "2020-03-31T08:00:33Z")

</div>

You can also use `\d` to represent the digits in your [regular expression](https://docs.python.org/2/library/re.html), and add `r"[-\d.]+"` if you also want to extract all negative numbers:

 ![2020-03-31 09_57_16-Grasshopper - 200330_ExtractNegativeIntegersAndFloats_00_](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/e/3/e367a39610ece12f83ed1abc503e693745cf2f7e.png)

[200330\_ExtractNegativeIntegersAndFloats\_00.gh](https://discourse.mcneel.com/uploads/short-url/lyspz4jI0caFbINTa5CdoWjM3te.gh) (5.0 KB)

---

<div class="post-metadata">

**Author:** ![rawitscher-torres](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/rawitscher-torres/32/159606_2.png) [@rawitscher-torres](https://discourse.mcneel.com/u/rawitscher-torres)\
**Post date:** [March 31, 2020, 8:39am UTC](https://discourse.mcneel.com/t/extract-numbers-from-text/99154/7 "2020-03-31T08:39:15Z")

</div>

for speed you could always use C# if your collection is very big. For a collection of 100,000 strings execution time is about 1sec in this 1 liner

```auto
 A = x.Select(a => a).Where(b => string.IsNullOrEmpty(b) == false)
      .Select(a => Regex.Replace(a, "[^.-9-9]", "")).ToArray();

```

Python will probably take ages… lol maybe one of you can test and prove me wrong 😄

 ![Capture1](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/8/2/829ad7ef5e2a2453e94b78c0eb9faac127eca933.png)

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [March 31, 2020, 8:56am UTC](https://discourse.mcneel.com/t/extract-numbers-from-text/99154/8 "2020-03-31T08:56:11Z")

</div>

> [@rawitscher-torres](#):
>
> Python will probably take ages… lol maybe one of you can test and prove me wrong 😄

Sure thing:

 ![2020-03-31 10_54_48-Grasshopper - 200330_ExtractNegativeIntegersAndFloats_01_](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/0/d/0d30c740652ee5d8f160c289802653c4cc73ea29.png)

[200330\_ExtractNegativeIntegersAndFloats\_01.gh](https://discourse.mcneel.com/uploads/short-url/apUWBxfMCgGXZ0hfqZWSvQXCLZo.gh) (8.6 KB)

Next time please provide a test case if you’re looking to compare options. Also, I suspect that running the regex expression on one multiline string will likely be more efficient (in Python at least).

---

<div class="post-metadata">

**Author:** ![gankeyu](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/gankeyu/32/487811_2.png) [@gankeyu](https://discourse.mcneel.com/u/gankeyu)\
**Post date:** [March 31, 2020, 10:08am UTC](https://discourse.mcneel.com/t/extract-numbers-from-text/99154/10 "2020-03-31T10:08:17Z")

</div>

A probably faster implementation

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/f/0/f07f368e3197bd9b2b13989feaf4e1b0485900b5.png)

---

<div class="post-metadata">

**Author:** ![rawitscher-torres](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/rawitscher-torres/32/159606_2.png) [@rawitscher-torres](https://discourse.mcneel.com/u/rawitscher-torres)\
**Post date:** [March 31, 2020, 11:29am UTC](https://discourse.mcneel.com/t/extract-numbers-from-text/99154/11 "2020-03-31T11:29:35Z")

</div>

The Regex class was actually causing performance issues, in this case. changed it to a more classical approach. They are roughly the same using the GH profiler turned on but when I use `Stopwatch` run time is much different `53-56 ms` Could you try to profile your version?

 ![Capture1](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/6/2/628b1bd23ae9026896dbaeadd562d75f999432e6.png)

```auto
private void RunScript(List<string> x, ref object A)
  {

    GH_Number [] formated = new GH_Number[x.Count];

    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

    sw.Start();
    for (int i = 0; i < x.Count; i++)
    {
      //string ne = Regex.Replace(x[i], "[^.-9-9]", "");
      string ne = new String(x[i].ToCharArray().Where(c => !char.IsLetter(c) && !char.IsUpper(c)).ToArray());
      double r = 0;
      if(double.TryParse(ne, out r))
        formated[i] = new GH_Number(r);

    }
    sw.Stop();

    Print(string.Format("Ellapsed time {0} ms", sw.ElapsedMilliseconds));
    A = formated;
  }

```

[200330\_ExtractNegativeIntegersAndFloats\_01.gh](https://discourse.mcneel.com/uploads/short-url/uQRD4t5E8NSA3AAlwx6GGg8mOMn.gh) (7.1 KB)

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [March 31, 2020, 1:12pm UTC](https://discourse.mcneel.com/t/extract-numbers-from-text/99154/12 "2020-03-31T13:12:48Z")

</div>

I suspect the additional profiling time you’re experiencing is due to [the casting cost of using type hints](https://www.grasshopper3d.com/forum/topics/ghpython-some-bottlenecks-bugs?), which can scale up quite quickly with large lists (removed the output to rule that out):

![2020-03-31 15_08_31-Grasshopper - 200330_ExtractNegativeIntegersAndFloats_01_](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/c/4/c4be20024e692ce7046fdf826e6e3f1a8fcc52ee.png)

---

<div class="post-metadata">

**Author:** ![rawitscher-torres](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/rawitscher-torres/32/159606_2.png) [@rawitscher-torres](https://discourse.mcneel.com/u/rawitscher-torres)\
**Post date:** [March 31, 2020, 1:58pm UTC](https://discourse.mcneel.com/t/extract-numbers-from-text/99154/13 "2020-03-31T13:58:47Z")

</div>

That is an important issue here that you pointed out. I will read more about your post when I have more time. But to be fair enough, for a real test this at the end this should be a compiled component in visual studio. Anyway lets suppose the real running time for this case is `52 ms`

By the way, removing the output did not affect performance. But, removing it wouldn’t make sense anyways, since having access to the data is what matters.

```auto
  GH_Number [] formated = new GH_Number[x.Count];

    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

    sw.Start();
    for (int i = 0; i < x.Count; i++)
    {
      if(x[i] is string)
      {
        string s = x[i] as string;
        //string ne = Regex.Replace(x[i], "[^.-9-9]", "");
        string ne = new String(s.ToCharArray().Where(c => !char.IsLetter(c) && !char.IsUpper(c)).ToArray());

        double r = 0;
        if(double.TryParse(ne, out r))
          formated[i] = new GH_Number(r);
      }

    }
    sw.Stop();

    Print(string.Format("Ellapsed time {0} ms", sw.ElapsedMilliseconds));
    A = formated;

```

 ![Capture1](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/2/8/282c9ea8628bfb772f112f0e9890ceefc6ec4d23.png)

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [March 31, 2020, 3:19pm UTC](https://discourse.mcneel.com/t/extract-numbers-from-text/99154/14 "2020-03-31T15:19:22Z")

</div>

> [@rawitscher-torres](#):
>
> By the way, removing the output did not affect performance.

Indeed, that’s what I was referring to with the _“… (removed the output to rule that out) …”_ bit.

> [@rawitscher-torres](#):
>
> But, removing it wouldn’t make sense anyways, since having access to the data is what matters.

Indeed, that’s where wrapping output data (as per the bottleneck thread) comes in.

> [@rawitscher-torres](#):
>
> … for a real test this at the end this should be a compiled component in visual studio.

Indeed, that’s where you’ll really start to demonstrate the benefit of a statically/strongly typed language.
