Search code examples
regexservicenow

Parsing WebLogic Cluster Using RegEx


I haver the below file that I need to parse out the hostname and port from each cluster.

End result should look like this for example:

Host = server1 Port = 40002

But I also need the ones with the FQDN

Host = server1.unix.domain.com Port = 40002

I don't know enough about regex to accomplish this.

WebLogicCluster SERVER1:40002,SERVER2:40002
WebLogicCluster SERVER1:40002,SERVER3:40002
WebLogicCluster SERVER1:40002,SERVER4:40002
WebLogicCluster SERVER1:8003,SERVER5.unix.domain.com:8003
WebLogicCluster SERVER1.unix.domain.com:8007,SERVER6.unix.domain.com:8007
WebLogicCluster SERVER1.unix.domain.com:8011,SERVER7.unix.domain.com:8011
WebLogicCluster SERVER1.unix.domain.com:8011,SERVER8.unix.domain.com:8011

Solution

  • You can try this:

    /[, ][^:]+:\d+/g
    

    Im not sure if that's what you want:

    const data = `WebLogicCluster SERVER1:40002,SERVER2:40002
    WebLogicCluster SERVER1:40002,SERVER3:40002
    WebLogicCluster SERVER1:40002,SERVER4:40002
    WebLogicCluster SERVER1:8003,SERVER5.unix.domain.com:8003
    WebLogicCluster SERVER1.unix.domain.com:8007,SERVER6.unix.domain.com:8007
    WebLogicCluster SERVER1.unix.domain.com:8011,SERVER7.unix.domain.com:8011
    WebLogicCluster SERVER1.unix.domain.com:8011,SERVER8.unix.domain.com:8011`
    
    const parse = data => {
      const regex = /(?: |,)(.*?):(\d*)/g, result = []
      let temp
      while (temp = regex.exec(data)){
        result.push({name: temp[1], port: temp[2]})
      }
      return result
    }
    
    console.log(parse(data))