![]() |
Regular expressions - How to parse user input on Diablo
Hello fellow developers
I am creating an app for tracking car (fueling, services, trips eg) and I need to validate input user gives me through gtkentry. I have managed to create needed gregex patterns after a day of work just to find out that glib on Diablo is 2.12 and GRegex is included since 2.14. So my question is if there is some way to validate entry with regular expressions other then GRegex class or there is a way to install GLib 2.14 or higher, or how should I validate user entry. If you find this question newbee it's because I have no experience writing linux code and I would be thankfull for your advise. ps. I using c and glib :) Johnnnie |
Re: Regular expressions - How to parse user input on Diablo
There doesn't look to be any obvious alternatives (you can find the GLib documentation at http://library.gnome.org/devel/glib/2.12/). Depending on the patterns you're trying to parse, the "Glob-style pattern matching" may work, otherwise you're down to low-level string functions (which gets complicated very quickly).
|
Re: Regular expressions - How to parse user input on Diablo
Hi Rob1n
thanks for an answer and can you point me to some "Glob-style pattern matching" documentation or examples because I have already "googleed" this and didn't find any helping info |
Re: Regular expressions - How to parse user input on Diablo
The documentation is on the page I gave you above (http://library.gnome.org/devel/glib/...-matching.html takes you directly). This would look to only give a TRUE/FALSE as to whether a string matches a pattern though, so may not be much help. It could present a starting point for string functions (http://library.gnome.org/devel/glib/...Functions.html) though (i.e. check which general format it matches before trying to pull out the relevant values).
|
Re: Regular expressions - How to parse user input on Diablo
Can you use pcre ? It seems to exist on the diablo repo at http://repository.maemo.org/pool/diablo/free/p/pcre3/
|
Re: Regular expressions - How to parse user input on Diablo
Quote:
in that case I have another question suppose I have input like this: some text "foo bar" and I would like to match just some text and not the text in quotation marks can you help me with proper regex because I am kind of lost ps I have regex which will extract text in "" "[\w\s]+" but I do not know how to do opposite thank you very much |
Re: Regular expressions - How to parse user input on Diablo
Well, I'll admit I haven't used libpcre in a program, but it stands for "perl compatible reg. ex.", and I have done a bit of perl, so I hope you won't mind if I answer based on my understanding of perl :)
This really depends on what you want to do, BTW! Anyway, suppose we have some text "foo bar" some other text If I match against the regex: Code:
^([^"]*)"[^"]*"(.*)$ If there are less than 2 "" quotes, though, you won't match at all, and so $1 and $2 will be empty. Also, if there's more than one set of quotes, then the second set will appear in $2, so a match against 'some "text" some "other" text' would set $1 to 'some ' and $2 to 'some "other" text', which might not be what you want. If you just want to match all text (possibly none) up to the first quote, then match against: Code:
^([^"]*) (the [^blah] is a 'negative character class' that matches against anything but (in that case) the letters b, l, a, h. Or in the example above, anything that's not a ".) When validating user input, I've found, where the user has unconstrainted ability to supply a string, it's better to be cautious and keep things simple. You might want to consider just rejecting their input until they supply something you're happy with. e.g: does it match Code:
^[\w\s]+$ If yes: make sure it's not too long ;) then process it.. If no: tell them no funny characters are allowed and try again :) http://regexkit.sourceforge.net/Docu...ntax.html#SEC1 for the pcre syntax.. http://regexkit.sourceforge.net/Docu...cre/index.html for the full docs. Hope that's enough to get you going.. |
All times are GMT. The time now is 00:40. |
vBulletin® Version 3.8.8