# User text fields: python string manipulation

**URL:** <https://discourse.mcneel.com/t/user-text-fields-python-string-manipulation/179468>\
**Category:** Scripting\
**Tags:** windows, python\
**Created:** [April 5, 2024, 3:53pm UTC](https://discourse.mcneel.com/t/user-text-fields-python-string-manipulation/179468 "2024-04-05T15:53:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![omichalek](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/omichalek/32/763609_2.png) [@omichalek](https://discourse.mcneel.com/u/omichalek)\
**Post date:** [April 5, 2024, 3:53pm UTC](https://discourse.mcneel.com/t/user-text-fields-python-string-manipulation/179468/1 "2024-04-05T15:53:13Z")

</div>

Using text fields, I would like to extract part of string (page name). I suppose the `re` library is not available [as in this example](https://stackoverflow.com/questions/67240210/how-to-extract-a-leading-number-from-a-string), is it?

Playing around with Text fields, this popped up:

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

Does it mean I need to use IronPython [as documented here](https://ironpython-test.readthedocs.io/en/latest/library/string.html) to manipulate strings?

---

<div class="post-metadata">

**Author:** ![scottd](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/scottd/32/494002_2.png) [@scottd](https://discourse.mcneel.com/u/scottd)\
**Post date:** [April 5, 2024, 4:09pm UTC](https://discourse.mcneel.com/t/user-text-fields-python-string-manipulation/179468/2 "2024-04-05T16:09:07Z")

</div>

Is this Rhino 7 or Rhino 8? Windows or Mac? In Rhino or in Grasshopper.

This works in Rhino 8 CPython 3:

```auto
#! python3

import rhinoscriptsyntax as rs
import scriptcontext as sc
import math

import System
import System.Collections.Generic
import Rhino
import re

def check_len(x):
  match = re.search(r'(\d+)(\w+)', x)
  return int(match[1])==len(match[2])

print(check_len('5hello'))
print(check_len('4rabbits'))

```

And this works in Rhino 7 and 8 IronPython 2:

```auto
import rhinoscriptsyntax as rs
import scriptcontext as sc
import math

import System
import System.Collections.Generic
import Rhino

import re
def get_num(raw_str):
    number = re.search(r'\d+',raw_str)
    return number.group(0)
    
print(get_num('5hello'))    
print(get_num('10HelloWorld')) 
print(get_num('16HelloWorldPython'))

```

---

<div class="post-metadata">

**Author:** ![Trav](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/trav/32/681734_2.png) [@Trav](https://discourse.mcneel.com/u/Trav)\
**Post date:** [April 5, 2024, 4:12pm UTC](https://discourse.mcneel.com/t/user-text-fields-python-string-manipulation/179468/3 "2024-04-05T16:12:41Z")

</div>

Yes you can use the IronPython string functions as well as .net string functions with Text Fields.

Such as something like the following example below would replace Page 1 with Wohoo 1

```auto
%<PageName().replace("Page","Wohoo")>%

```

We have a few examples here in the help file as well.

[https://docs.mcneel.com/rhino/8/help/en-us/index.htm#information/text\_fields.htm](https://docs.mcneel.com/rhino/8/help/en-us/index.htm#information/text_fields.htm)

---

<div class="post-metadata">

**Author:** ![omichalek](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/omichalek/32/763609_2.png) [@omichalek](https://discourse.mcneel.com/u/omichalek)\
**Post date:** [April 10, 2024, 11:16am UTC](https://discourse.mcneel.com/t/user-text-fields-python-string-manipulation/179468/4 "2024-04-10T11:16:55Z")

</div>

> [@scottd](#):
>
> Is this Rhino 7 or Rhino 8? Windows or Mac? In Rhino or in Grasshopper.

I meant straight in Rhino 8 layouts
