如何解析来自多个字符串文本? [英] How to parse text from multiple strings?

查看:146
本文介绍了如何解析来自多个字符串文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的终端模拟器库来创建一个终端,然后我用它来发送进入通过串行到串行设备的数据。当该数据被发送回欲分析它,并显示在一个EDITTEXT用户最重要的信息。目前,我收到字节数组/块,我将它们转换为字符串。当我得到一个\\ r或\\ n我克里特岛一个新的字符串并重复上述过程。这是对大多数命令,但有些命令,多行像秀VLAN在这里返回结果:

I am using a terminal emulator library to create a terminal and then I use it to send the data entered over serial to a serial device. When the data is sent back I want to parse it and show the most important information to the user in an editText. Currently I receive byte arrays/chunks and I convert them to a string. When I get a \r or a \n I crete a new string and the process repeats. This is fine for most commands, however some commands return results over multiple lines like "show vlan" here:

当我遍历这个,我得到的每一行的字符串。首先将包含VLAN名称状态和端口,作为一个例子。所以现在我有一个问题,我怎么能VLAN 1拥有的Active X端口。它们在不同的串。这里是code和截图的电流更容易命令,其中我感兴趣的一行:

When I loop through this I get a string for each line. The first would contain VLAN Name Status and Ports, as an example. So now I have a problem, how can I VLAN 1 has x ports active. They are in different strings. Here is the code and screenshot for a current easier command where I am interested in one line:

Handler viewHandler = new Handler();
Runnable updateView = new Runnable() {
    @Override

    public void run() {

        mEmulatorView.invalidate();

        if (statusBool == true) {
            for (int i = 0; i < dataReceived.length(); i++) {
                parseCommand = parseCommand + dataReceived.charAt(i);

 if (dataReceived.charAt(i) == '\n' || dataReceived.charAt(i) == '\r'){

    if(parseCommand.contains("KlasOS"))
            {

        String[] tokens = parseCommand.split("\\s{1,}");

        final String ReceivedText = mReceiveBox.getText().toString() + " "
        + new String("Software Version: " + tokens[1] + "\n" );

        runOnUiThread(new Runnable() {
            public void run() {
                                mReceiveBox.setText(ReceivedText);
                                mReceiveBox.setSelection(ReceivedText.length());

                            }
                        });         

                            }

                parseCommand = "";

            }               
            }

            statusBool = false;
            viewHandler.postDelayed(updateView, 1000);
        }
    }
};

现在我想改变这个,所以我可以处理多行。请问EBST方式来存储串是否含有某种信息?

Now I would like to change this so i can deal with multiple lines. Would the ebst way be to store strings if they contain certain information?

我需要这个右手EDITTEXT输出:

I need this outputted on the right hand editText:

下面的端口是VLAN 1:FA1 / 0,FA1 / 1,FA1 / 2,FA1 / 3,FA1 / 4,FA1 / 5,FA1 / 6,FA1 / 7,FA1 / 8,FA1 / 9,FA1 / 10,FA1 / 11,GI0

"The following ports are on vlan 1: Fa1/0, fa1/1, fa1/2, fa1/3, fa1/4, fa1/5, fa1/6, fa1/7, fa1/8, fa1/9, fa1/10, fa1/11, Gi0"

推荐答案

基本上,你需要一种方法来可靠地检测命令结果的末尾。然后把它归结为发送您的命令,从设备中读取数据,直到你遇到结果的末尾,最后分析这一结果。

Basically, you need a way to reliably detect the end of a command result. Then it boils down to sending your command, reading data from the device until you encounter the end of result, and finally parsing that result.

我就扫描的提示(开关#),你在你自己的答案做。也许你甚至能够迫使装置使用一个更特殊的字符序列,这是不太可能在命令中的常规输出发生并且可以更容易地检测结果的结束。例如,您可以尝试的提示配置包括像^ G或^升的控制字符。或者,如果用户不介意的话,你总是可以发送发出这样的序列,例如,第二个命令显示VLAN;回声^ G。

I would scan for the prompt (switch#) as you do in your own answer. Maybe your are even able to force the device to use a more peculiar character sequence, which is unlikely to occur in the regular output of commands and makes it easier to detect the end of a result. For example, you could try to configure the prompt to include a control character like ^G or ^L. Or if your users don't mind, you could always send a second command that emits such a sequence, for example, "show vlan; echo ^G".

您还应该ppared为命令错误,这会导致不同的输出$ P $,例如,更多或预期或完全不同的输出格式更少的行。一个结果甚至可能同时包含,常规输出和一个警告或错误。

You should also be prepared for command errors, which result in a different output, for example, more or fewer lines as expected or a totally different output format. A result may even contain both, a regular output and a warning or an error.

这篇关于如何解析来自多个字符串文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆