Finding 3 consecutive numbers in a computer name

We use different naming conventions for computers. Some of them have LLLLNNN and others have LLLNNN-N and other combinations but the commonality is that every computer name has NNN in them. Is there a way to find just the 3 digit numbers in a computer name and parse them out to use somewhere else? In the past I have used separate relevance for each but would really prefer one that identifies the 3 digit number in all of them.

Before @JasonWalker comes in here with his regex skills, here’s an approach you could try:

q: concatenation of ((characters of "abc123def" as integer) as string)
A: 123
T: 0.171 ms
I: singular string

computer name will return a string, so you should be able to use this method, just substituting my string “abc123” with (computer name). The idea is that we’re telling it to take each individual character of the string and cast it as an integer. Since we’re using plural relevance, it just drops the ones that can’t be cast. Then we convert each returned integer back to a string and concatenate so they’re back together in a single string.

2 Likes

I’ll take that challenge, @alinder :slight_smile:

I like your approach, it’s clean and easy-to-read. But there’s an edge case for NNN-N, where it will return all four numbers together. To get around that, I’d take just the first three matching numbers. I’m on a phone now and can’t validate but I think this should work -

q: first 3 of concatenation of ((characters of "abc123def" as integer) as string)

The regex method is probably overkill here, but this should work too -
matches(regex("[[:digit:]]{3}")) of computer name

1 Like

I see so basically you are asking to combine the 3 number characters which are the only ones that get returned because you are using as integer (the letter characters simply are ignored). Is that about right?

Correct, though as Jason astutely points out, in the case of LLLNNN-N, my solution would have returned NNNN, so he has it only returning the first 3 characters

This works great thanks guys!

first 3 of concatenation of ((characters of (computer name) as integer) as string)

2 Likes