Skip to main content
added 37 characters in body
Source Link
Farid Cheraghi
  • 8.8k
  • 1
  • 24
  • 54

Standalone code

def f(s):
    idx = s.upper().index('S')
    s = s[:idx]
    for i in reversed(range(len(s))):
        if s[i].isalpha():
            s = s[i+1:]
            break
    if s.isdigit():
        return int(s)
    else:
        return 0

Testing

print f("S2Olch1E")
#0
print f("6S2Olch1E")
#6
print f("5B66S2Olch1E")
#66
print f("54F5456B666S2Olch1E")
#666

FieldIn your expression (Field Calculator), just use:

f(!fieldName!)

Standalone code

def f(s):
    idx = s.upper().index('S')
    s = s[:idx]
    for i in reversed(range(len(s))):
        if s[i].isalpha():
            s = s[i+1:]
            break
    return s

Testing

print f("6S2Olch1E")
print f("5B66S2Olch1E")
print f("54F5456B666S2Olch1E")

Field Calculator

f(!fieldName!)

Standalone code

def f(s):
    idx = s.upper().index('S')
    s = s[:idx]
    for i in reversed(range(len(s))):
        if s[i].isalpha():
            s = s[i+1:]
            break
    if s.isdigit():
        return int(s)
    else:
        return 0

Testing

print f("S2Olch1E")
#0
print f("6S2Olch1E")
#6
print f("5B66S2Olch1E")
#66
print f("54F5456B666S2Olch1E")
#666

In your expression (Field Calculator), just use:

f(!fieldName!)
Source Link
Farid Cheraghi
  • 8.8k
  • 1
  • 24
  • 54

Standalone code

def f(s):
    idx = s.upper().index('S')
    s = s[:idx]
    for i in reversed(range(len(s))):
        if s[i].isalpha():
            s = s[i+1:]
            break
    return s

Testing

print f("6S2Olch1E")
print f("5B66S2Olch1E")
print f("54F5456B666S2Olch1E")

Field Calculator

f(!fieldName!)