Reputation: 5974
I am entering the command show vlan
and receiving data in this format, it is one string:
switch# show vlan
VLAN Name Status Ports
---- -------------------------------- --------- -------------------------------
1 default Active Fa1/0, Fa1/1, Fa1/4, Fa1/6
Fa1/7, Fa1/8, Fa1/9, Fa1/10
Fa1/11, Gi0
2 VLAN0002 Active
3 VLAN0003 Active Fa1/5
6 VLAN0006 Active
999 VLAN0999 Active Fa1/2, Fa1/3
switch#
I wish to make new strings that start with the number of the vlan and end when it reaches the VLAN number. So the Strings here would be:
(1) 1 default Active Fa1/0, Fa1/1, Fa1/4, Fa1/6
Fa1/7, Fa1/8, Fa1/9, Fa1/10
Fa1/11, Gi0
(2) 2 VLAN0002 Active
(3) 3 VLAN0003 Active Fa1/5
(4) 6 VLAN0006 Active
(5) 999 VLAN0999 Active Fa1/2, Fa1/3
What would I do here, a regex that tests for a number followed by a white space but with a \r or \n before it? Or similar in java using .charAt(i) etc? and then substring it?
The I need to split each of these strings into tokens with a white space delimiter. I would use a for loop to go through each string I guess.
I tried splitting at \r\n if it is followed by a digit, was this right?
String[] parts = finalCommand.split("\r\n?=\\d");
but it doesn't work?! I really can't see why.
Upvotes: 0
Views: 220
Reputation: 75232
I don't think split()
is the tool you're looking for. Check this out:
List<String> matchList = new ArrayList<String>();
Pattern p = Pattern.compile("(?m)^\\d+.*$(?:\r?\n(?!\\S).*$)*");
Matcher m = p.matcher(s);
while (m.find()) {
matchList.add(m.group());
}
As you said, continuation lines are characterized by whitespace following the line separator. This regex identifies them by doing a negative lookahead for a non-whitespace character (\S
) immediately after the line separator.
The (?m)
turns on multiline mode, allowing ^
and $
to match the beginning and end of individual lines. Note that it's the carriage return (\r
) that's optional in the line separator, not the linefeed (\n
).
Upvotes: 1
Reputation: 5974
Answer was pretty simple, was some sort of brackets problem
This doesn't work:
String[] parts = finalCommand.split("\r\n?=\\d");
This does:
String[] parts = finalCommand.split("(\r\n)(?=\\d)");
Upvotes: 0
Reputation: 183321
I tried splitting at \r\n if it is followed by a digit, was this right?
String[] parts = finalCommand.split("\r\n?=\\d");
but it doesn't work?! I really can't see why.
If finalCommand
is after you've already stripped off the leading #switch\r\n
and trailing \r\nswitch#
, and if the lines in finalCommand
are indeed separated by \r\n
, then — yes, that's almost right. The only thing you need to change is, you need to write (?=\d)
rather than ?=\d
: the parentheses are part of the syntax for a lookahead assertion. (Otherwise \n?=
is interpreted as "an optional \n
, followed by =
.) So:
String[] parts = finalCommand.split("\r\n(?=\\d)");
Upvotes: 1