View Single Post
Posts: 236 | Thanked: 223 times | Joined on Oct 2009 @ NE UK
#7
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:
^([^"]*)"[^"]*"(.*)$
then after the match, $1 contains 'some text ' and $2 contains ' some other text'. ($1 and $2 are the first and second matched substrings).

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:
^([^"]*)
After this, $1 would contain 'some text ' (note the trailing space!)

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

Last edited by kwotski; 2009-12-11 at 23:31.