解析自定义标签以获取属性以进行数据解析
您好,我正在寻找清理标签或至少从文本中的自定义标签中获取数据的最佳实践或想法。
我确信我可以编写某种“解析器”来手动遍历每一行,但是今天没有一些聪明的方法吗?
数据思考:
{电话:555-123456789}
这里我们将“电话”作为键,将号码作为数据。看起来很像 JSON 格式,但对于人类来说更容易编写。
或者也
{link: article123456 ; title: Read about article 123456 here }
可能是正常的(X)HTML:
<a href="article123456.html" > Read about article 123456 here </a>
人类并不总是愿意“修剪”他们的输入,而且用懒惰的所见即所得编辑器制作的旧网站也不是这样,所以我首先需要弄清楚哪些对属于一起,然后在找到“内的数据”然后修剪结果。
问题在于上面的“标题”部分,标题文本周围没有“”,因此它可以自动添加它们或向人类显示错误。
关于如何以最佳方式获取这些数据有什么想法吗?似乎有几种方法可能有效,但是解决这个问题的最佳方法是什么?
HI there, I am looking for best practice or ideas for cleaning tags or at least grabbing the data from within custom tags in a text.
I am sure I can code some sort of "parser" that will go through every line manually, but isnt there some smartere way today?
Data thoughts:
{Phone:555-123456789}
here we have "phone" being the key and the number as the data. Looks a lot like JSON format but its easier to write for a human.
or
{link: article123456 ; title: Read about article 123456 here }
Could be normal (X)HTML too:
<a href="article123456.html" > Read about article 123456 here </a>
Humans aren't always nice to "trim" their input and neither are old websites made with lazy WYSIWYG editors, so I first need to figure out which pairs belongs together and then after finding the "data within" then trim the results.
Problem is in the "title" part above, that there are no " " surrounding the title-text, so it could either add them automatically or show the error to the human.
Any thoughts on how to grab these data the best way? There seems to be several ways that might work, but whats your best approach to this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我首先会为我正在解析的数据的语法编写一个“标记器”。分词器是一个(相对)简单的过程,它将字符串分解为一系列片段或标记。例如,在前两种情况下,您的基本标记将包含:“{”、“}”、“:”、“;”,其他所有内容都将被解释为数据标记。这可以通过循环、递归函数或许多其他方式来完成。对第二个示例进行标记将生成一个具有以下值的数组(或某种其他类型的列表):
下一步将是“清理”您的数据,尽管在这些情况下,真正意味着删除不需要的空格。迭代生成的标记数组,并更改每个标记,以便不存在开头或结尾空格。此步骤可以与标记化结合起来,但我认为单独进行会更干净、更清晰。然后,您的令牌将如下所示:
最后是实际的“解释”。您需要将标记数组转换为您想要成为解析过程的最终产品的任何类型的实际数据结构。为此,您肯定需要一个递归函数。如果在数据标记上调用该函数,后跟冒号标记,然后是数据标记,它将以键值对解释它们,并相应地生成数据结构。如果在一系列带有分号标记的标记上调用它,它将在每个分号处拆分标记,并在每个结果组上调用自身。如果对大括号标记中包含的标记进行调用,它将在执行其他操作之前对所包含的标记调用自身。请注意,这不一定是您要检查这些不同情况的顺序;特别是,如果您打算嵌套大括号(或任何其他类型的分组标记,例如方括号、尖括号或圆括号),接下来您需要确保以正确的嵌套顺序解释这些标记。
这些过程的结果将是您想要的任何类型的完全解析的数据结构。请记住,此过程假设您的数据全部隐式存储为字符串类型;如果您希望“3”和 3 有不同的解释,那么事情会变得更复杂一些。我概述的这种方法根本不是唯一的方法,但它是我解决问题的方法。
I would first write a "tokenizer" for the syntax of the data I was parsing. A tokenizer is a (relatively) simple process that breaks a string down into a series of fragments, or tokens. For example, in your first two cases your basic tokens would consist of: "{", "}", ":", ";", and everything else would be interpreted as a data token. This can be done with a loop, a recursive function, or a number of other ways. Tokenizing your second example would produce an array (or some other sort of list) with the following values:
The next step would be to "sanitize" your data, though in these cases all that really means is removing unwanted whitespace. Iterate through the token array that was produced, and alter each token so that there is no beginning or ending whitespace. This step could be combined with tokenization, but I think it's much cleaner and clearer to do it separately. Your tokens would then look like this:
And finally, the actual "interpretation." You'll need to convert your token array into whatever sort of actual data structure that you intend to be the final product of the parsing process. For this you'll definitely want a recursive function. If the function is called on a data token, followed by a colon token, followed by a data token, it will interpret them at a key-value pair, and produce a data structure accordingly. If it is called on a series of tokens with semicolon tokens, it will split the tokens up at each semicolon and call itself on each of the resulting groups. And if it is called on tokens contained within curly-brace tokens, it will call itself on the contained tokens before doing anything else. Note that this is not necessarily the order in which you'll want to check for these various cases; in particular, if you intend to nest curly-braces (or any other sort of grouping tokens, such as square brackets, angle brackets, or parentheses), you'll next to make sure to interpret those tokens in the correct nested order.
The result of these processes will be a fully parsed data structure of whatever type you'd like. Keep in mind that this process assumes that your data is all implicitly stored as the string type; if you'd like "3" and 3 to be interpreted differently, then things get a bit more complicated. This method I've outlined is not at all the only way to do it, but it's how I'd approach the problem.