User text fields: python string manipulation

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, is it?

Playing around with Text fields, this popped up:

Does it mean I need to use IronPython as documented here to manipulate strings?

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

This works in Rhino 8 CPython 3:

#! 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:

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'))
1 Like

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

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

1 Like

I meant straight in Rhino 8 layouts