regex: JavaScript RegEx to boost::regex conversion
Hello, I have problem creating case less, global match and multi line regexes in boost and I'd like to ask for a few examples of equivalent (Javascript's RegEx <-> boost::regex) regex test js: string.match( /^abc/ ) string.match( /^abc/i) string.match( /^abc/g) string.match( /^abc/m) c++ regex_match(string, regex("^abc"), match_default) regex_match(string, regex("^abc"), match_default | icase) // <- is it correct? regex_match(string, regex("^abc"), ?? ) //global match? regex_match(string, regex("^abc"), ?? ) //multi line? also I'd like to ask what are the proper namespaces/typedefs/enums I have to use for match flags (for example, there's boost::regex_constants::icase and boost::regex::icase etc...) Thank you
I have problem creating case less, global match and multi line regexes in boost and I'd like to ask for a few examples of equivalent (Javascript's RegEx <-> boost::regex) regex test
js: string.match( /^abc/ ) string.match( /^abc/i) string.match( /^abc/g) string.match( /^abc/m)
c++
First off remember that regex_match doesn't *search*, it succeeds only if the whole of the expression matches the whole of the text, so you really need to be using regex_search.
regex_match(string, regex("^abc"), match_default) regex_match(string, regex("^abc"), match_default | icase) // <- is it correct?
No, regex_search(whatever, regex("^abc", regex::perl|regex::icase));
regex_match(string, regex("^abc"), ?? ) //global match?
Use regex_iterator or regex_token_iterator to enumerate through several matches in one string.
regex_match(string, regex("^abc"), ?? ) //multi line?
Boost.Regex will always match ^ against embedded newlines.
also I'd like to ask what are the proper namespaces/typedefs/enums I have to use for match flags (for example, there's boost::regex_constants::icase and boost::regex::icase etc...)
Those nested within boost::regex are for use with the regex constructor, and determine the regular expression syntax, and case sensitivity. Those that are of type boost::regex_constants::match_flag_type are for passing to regex_match/regex_search/regex_iterator etc. Finally in the next release, the Perl-like (?imsx-imsx) syntax will be supported, which makes all this a little easier... John.
John Maddock wrote: thanks for your reply. Now it's a bit clear for me. s.match( /^abc/ ) -> regex_match(s,regex("^abc"), match_default| match_single_line) s.match( /^abc/m ) -> regex_match(s,regex("^abc"), match_default) s.match( /^abc/i ) -> regex_match(s,regex("^abc"), icase)
First off remember that regex_match doesn't *search*, it succeeds only if the whole of the expression matches the whole of the text, so you really need to be using regex_search.
global is of course for search / replace, my mistake sorry
regex_match(string, regex("^abc"), match_default) regex_match(string, regex("^abc"), match_default | icase) // <- is it correct?
both aren't correct (need match_single_line)
participants (2)
-
John Maddock
-
pps