解析具有特殊字符的用户输入的良好做法?
大家好,我是反抗邪恶银河帝国的节目作家。现在,我们正在准备对死星进行大胆的攻击。
我创建了一个庞大的数据库,其中包含有关即将攻击死星的 X 翼和 Y 翼战斗机上的飞行员、机器人和系统的信息。由于我们迫切需要快速访问信息,因此我设计了一个系统,用户可以在其中输入一串文本来指定他们想要检索的数据。
用户可以通过许多字段进行搜索。例如,如果用户想要查找有关 R2D2 的数据,他可以输入查询“DROID:R2D2”并快速找到该数据。如果他需要有关红队所有飞行员、机器人和系统的数据,他可以输入“TEAM:RED TEAM”。
然而,该系统旨在处理更复杂的查询。从前面的查询中可以看到,数据字段名称和数据值由冒号 :
字符分隔。用户必须能够搜索每个字段的多个值并同时搜索多个字段。
这是我的更高级用户的典型查询: “船舶类型:X-WING,Y-WING;位置:坞站 94;DROIDTYPE:R2”
我对处理此问题的方式非常满意。 我使用 String.Split(';')
函数读取以获取用户选择的三个数据字段的值。我使用 String.Split(':') 将数据字段名称与值分开,然后使用 String.Split(',')` 来获取用户拥有的不同字段值输入了不止一个。您可以看到这是如何工作的。
然而,有一个问题。莉亚公主(你知道她有多挑剔)坚持在她的爆能枪校准记录中使用“:”、“;”和“,”。当她尝试查询这些数据时,我的程序无法理解她的查询。她说,如果这个问题不得到纠正,死星很快就会摧毁叛军基地。
因此,我请求您帮助建议解析用户输入的良好实践。我怎样才能让我的程序为莉亚公主工作?
更新我将推测我自己问题的答案。当我学习使用 C 风格的语言进行编程时,我了解了“转义序列”,我可以在其中指定字符串文字中的普通保留字符。 (例如,“字符通常终止字符串文字,但我可以使用\”“转义”它)我猜对我的程序做一些类似的事情是个好主意,但我不知道会发生什么是实现它的最佳方式。在准备这次死星袭击之前,我的大部分编程经验都是使用水分蒸发器的二进制语言,所以这对我来说是新的。
Greetings, I am the program writer for the rebellion against the evil galactic empire. Right now, we are preparing for a daring assault on the Death Star.
I have created a vast database of information about our pilots, droids, and systems aboard our X-Wing and Y-Wing fighters that will soon assault the Death Star. Because of our critical needs to access the information quickly, I have designed a system where a user can enter a string of text to specify what data they want to retrieve.
There are numerous fields the user may search by. For example, if a user wants to find data about R2D2, he could type in the query "DROID:R2D2" and quickly find this data. If he needs the data about all the pilots, droids, and systems for Red Team, he could enter "TEAM:RED TEAM".
However, the system is designed to handle more complex queries. As you can see from the previous query, the data field name and data value are seperated by a colon :
character. The users must be able to search multiple values per field and search multiple fields all at once.
Here is a query typical of my more advanced users:
"SHIPTYPE:X-WING,Y-WING; LOCATION:docking bay 94; DROIDTYPE:R2"
I am quite happy with the way I have handled this.
I use the String.Split(';')
function to read to get the values of the three data fields the user has selected. I use String.Split(':') to seperate the data field name from the value, and then I user
String.Split(',')` to get the different field values where the user has entered more than one. You can see how this works.
However, there is one problem. Princess Leiah (You know how fussy she is) insists on using ':', ';', and ',' in her blaster calibration records. When she tries to query this data, her queries are unintelligible to my program. She says that the Death Star will destroy the rebel base soon if this problem is not corrected.
Therefore, I ask for you help in suggesting good practices for parsing user inputs. How can I get my program working for Princess Leiah?
Update I will speculate about the answer to my own question. When I learned to program in C-flavored languages, I learned about "escape-sequences" where I can specify an ordinarily reserved character in my string literals. (eg, A " character ordinarily terminates a string literal, but I can "escape" it using \") I'm guessing that it would be a good idea to do something similiar with my program, but I don't know what would be the best way of accomplishing it. Prior to preparing for this Death Star assault, most of my programming experience has been with the binary language of moisture vaporators, so this is new to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与 Google 类似,您可以允许将搜索词括在“标记中。因此您可以允许:
SHIPTYPE:"BATTLESTAR:GALACTICA"; LOCATION:docking bay 94; DROIDTYPE:R2
In similar fashion to Google, you could allow for search terms to be enclosed in " marks. So you'd allow:
SHIPTYPE:"BATTLESTAR:GALACTICA"; LOCATION:docking bay 94; DROIDTYPE:R2
很抱歉,星球大战的描述无助于澄清您的问题。
无论如何,我已经写了很多解析代码了。我不太关心 String.Split() 除非数据总是这样格式化。 http://www.blackbeltcoder.com/Articles/strings/a-text-parsing-helper-class" rel="nofollow">有一个通用解析器类。 blackbeltcoder.com/Articles/strings/a-text-parsing-helper-class。我可能会使用类似的东西来实现更灵活的解析器。
I'm sorry but the Star Wars description didn't help to clarify your question.
At any rate, I've written a lot of parsing code. I don't care much for
String.Split()
unless the data will always be formatted just so. There is a general-purpose parser class at http://www.blackbeltcoder.com/Articles/strings/a-text-parsing-helper-class. I would probably use something like that to implement a more flexible parser.看来您在分隔符的左侧使用关键字,而在分隔符的右侧使用实际查询值。您可以尝试检查
;
和:
之间包含的word
是否是实际关键字,如果不是忽略此时的分隔符。然而,这并不简单,也不能仅与 String.Split() 一起使用。
It seems that you are using Keywords on the left side of the delimiter and the actual query value on the right side of the delimiter. You could try something where you check to see if the
word
contained between the;
and the:
is a actual keyword and if it isn't ignore the delimiter at that point.However that won't be simple, nor will it work solely with
String.Split()