Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good practices for parsing user inputs with special characters?

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 userString.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.

like image 848
Vivian River Avatar asked Feb 28 '11 16:02

Vivian River


1 Answers

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

like image 185
Jon Egerton Avatar answered Sep 24 '22 10:09

Jon Egerton