0
<189>131: *Jan  2 13:20:47.581: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to up

From the above String, I want to split the the following:

 189  -between <>
    Jan  2 13:20:47.581  -between * %
    LINEPROTO 
    5
    UPDOWN
    FastEthernet0/1 - after string Interface
    up  -after String state to status 

Final String array will be:

[189],[Jan  2 13:20:47.581],[LINEPROTO],[5],[UPDOWN],[FastEthernet0/1],[up]

How do I do this using java regex?

yshavit
  • 42,327
  • 7
  • 87
  • 124

2 Answers2

0

This regex will capture the groups that you required:

<(.+)>.+?[*](.+?)%(LINEPROTO)-(.+?)-(.+?):.+?Interface (.+?), changed state to (.+)

The groups are (separated by commas):

189,Jan 2 13:20:47.581:,LINEPROTO,5,UPDOWN,FastEthernet0/1,up

Note that Jan 2 13:20:47.581: includes the ':' since you indicated that you wanted everything between * and %.

Explanation of the regex:

  • () are groups.
  • +? is non-greedy
  • [*] is the '*' character rather than the regex operator.

Also, I assume that you didn't want the literal 'LINEPROTO' (even though your instructions seemed to indicate you do). In which case you will want:

<(.+)>.+?[*](.+?)%(.+)-(.+?)-(.+?):.+?Interface (.+?), changed state to (.+)

Update based on comment

Updated regex is:

<(.+)>.+?[*](.+?): %(.+)-(.+?)-(.+?):.+?Interface (.+?), changed state to (.+)

Groups will be: 189, Jan 2 13:20:47.581, LINEPROTO, 5, UPDOWN, FastEthernet0/1, up

acarlon
  • 16,764
  • 7
  • 75
  • 94
-1

You'll want to look into JavaScript's substr or split..Should help you out.

sTg
  • 4,313
  • 16
  • 68
  • 115