Data Infrastructure Management Software Discussions

Highlighted

RegExp for extracting host names from switch alias

Hi

 

I have a  problem with RegExp for extracting host names from  switch alias.

Details:

switchalias : abcdtsm01_aixfcs3  

Hostname :  abcd-tsm01

Expression used: ([a-zA-Z0-9]*)_([^_]+).*    \1           it will capture   " abcdtsm01" which is wrong

How can i add  "-" in the middle  to show as   abcd-tsm01.

 

 

4 REPLIES 4
Highlighted

Re: RegExp for extracting host names from switch alias

Are all the strings the same, meaning four letters and then - or are they different.

 

regex is to pull out what you need, but string manipulation is different

View solution in original post

Highlighted

Re: RegExp for extracting host names from switch alias

Are all the hosts exactly 4letters-xxxxx?

 

Basically, you have an alphanumeric string - how do you know where the dash is supposed to be?

Highlighted

Re: RegExp for extracting host names from switch alias

Hi

Few Hostnames  same four letters and then  "-" . 

Highlighted

Re: RegExp for extracting host names from switch alias

If it is always 4 characters, dash, ...

 

([A-Za-z0-9]{4})([A-Za-z0-9]+)-.*

 

This regular expression will:

Look for exactly 4 alphanumeric case insensitive characters - this will be \1 aka string 1

Look for at least a 2 alphanumeric case insensitive string that follows - this will be \2 aka string 2

Look for a dash, follow by more characters

 

When this pattern matches, you will have 2 strings

 

If you change the format for this rule from "\1" to "\1-\2" , you will build a concatenated string of "string1-string2"

 

I would characterize this regexp as fairly risky - it is only worth trying if ALL of your hosts are named this way.

 

If this pattern only applies for TSM hosts, you will want to write a narrower regexp

Check out the KB!
Knowledge Base
All Community Forums