Let's try on the simple example
2> "\q".
"q"
3> "\\q".
"\\q"
But do really "\\q" means 3 symbols or 2 ?
6> lists:map(fun(X) -> erlang:display(X) end, "\\q").
92
113
[true,true]
Well. So it's in your regular expression you put exactly 2 characters -
\ and [
which is required by a regexp to interpret [ as a character and not as
class start symbol.
So it works quite predictable ;)
/Gaspar
--
Gaspar Chilingarov
tel +37493 419763 (mobile - leave voice mail message)
icq 63174784
skype://gasparch
e mailto:n...@web.am mailto:gasp...@gmail.com
w http://gasparchilingarov.com/
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions
Note
The Erlang literal syntax for strings give special meaning to the "\"
(backslash) character. To literally write a regular expression or a
replacement string containing a backslash in your code or in the shell,
two backslashes have to be written: "\\".
/Sverker, Erlang/OTP Ericsson
[{C}||C<-"\\q"].
output:
[{92},{113}]
Zvi
Gaspar Chilingarov wrote:
>
> 6> lists:map(fun(X) -> erlang:display(X) end, "\\q").
> 92
> 113
> [true,true]
>
--
View this message in context: http://www.nabble.com/Re%3A-Why-is-it-necessary-to-%22double-escape%22--%09characters-in-regular-expressions--tp22777475p22780122.html
Sent from the Erlang Questions mailing list archive at Nabble.com.
1> [[C]||C<-"\\q"].
["\\","q"]
2> [{C}||C<-"\\q"].
[{92},{113}]
Zvi
Gaspar Chilingarov wrote:
>
> 6> lists:map(fun(X) -> erlang:display(X) end, "\\q").
> 92
> 113
> [true,true]
>
--
View this message in context: http://www.nabble.com/Re%3A-Why-is-it-necessary-to-%22double-escape%22--%09characters-in-regular-expressions--tp22777475p22780162.html
1> [[C]||C<-"\\q"].
["\\","q"]
2> [{C}||C<-"\\q"].
[{92},{113}]
Zvi
Gaspar Chilingarov wrote:
>
> 6> lists:map(fun(X) -> erlang:display(X) end, "\\q").
> 92
> 113
> [true,true]
>
--
View this message in context: http://www.nabble.com/Re%3A-Why-is-it-necessary-to-%22double-escape%22--%09characters-in-regular-expressions--tp22777475p22780162.html
Sent from the Erlang Questions mailing list archive at Nabble.com.
_______________________________________________
Using \ to excape brackets seems to vary between different
implementations of regexps that I look at.
As for the orginial question, others have already pointed it out, but in
order to get a \ in the actual string you create, you need to put a
double \ in the literal. And that's escaping. :-)
Johnny
Richard Andrews wrote:
> IIRC the way to escape [ in regular expressions is [[] not \[.
> Similarly []] not \].
>
> Never tried with erlang re application though.
>
> ------------------------------------------------------------------------
> *From:* David Mitchell <monc...@gmail.com>
> *To:* erlang-questions Questions <erlang-q...@erlang.org>
> *Sent:* Monday, 30 March, 2009 2:19:28 PM
> *Subject:* [erlang-questions] Why is it necessary to "double-escape" [
> characters in regular expressions?
>
> Hello group,
>
> Running 5.6.5 under Windows...
>
> I've got a bunch of code that's "almost but not quite syntactically
> correct" XML, and I'm trying to convert it to valid XML. Part of this
> process involves removing some invalid CDATA tags.
>
> My code fragment:
> re:replace("abc123", "<!\[CDATA\[<", "<", [{return, list}]).
> is giving me "exception error: bad argument in function re:replace/4.
>
> Trial and error shows that removing the escaped [ characters:
> re:replace("abc123 <![CDATA[< abc123", "<!CDATA<", "<" [{return, list}]).
> works as expected, but it's obviously not what I want.
>
> However, "double-escaping" the [ characters (by adding a second \ prior
> to the [ character) does exactly what I want:
> re:replace("abc123 <![CDATA[< abc123", "<!\\[CDATA\\[<", "<",
> [{return, list}])
> returns "abc123 < abc123", which is the result I'm after.
>
> In this context, I guess it's conceivable that the [ character can be
> misinterpreted in two distinct ways in a regular expression:
> - it could denote the start of an Erlang list
> - it could denote the start of a character grouping within a regular
> expression
> However, I didn't expect that "double escaping" it would be the solution
> to my problem.
>
> Is this expected behaviour, or some sort of anomaly? In any case,
> sending this email to the mailing list should help out the next person
> who falls into this trap, but who can use Google to track down the
> solution...
>
> Regards
>
> David Mitchell
>
> ------------------------------------------------------------------------
> Enjoy a better web experience. Upgrade to the new Internet Explorer 8
> optimised for Yahoo!7. Get it now.
> <http://au.rd.yahoo.com/search/ie8/mailtagline/*http://us.lrd.yahoo.com/_ylc=X3oDMTJxbnQwdTJhBF9zAzIxNDIwMjU2NTkEdG1fZG1lY2gDVGV4dCBMaW5rBHRtX2xuawNVMTEwMzQ0OAR0bV9uZXQDWWFob28hBHRtX3BvcwN0YWdsaW5lBHRtX3BwdHkDYXVueg--/SIG=11k6t9t1c/**http://downloads.yahoo.com/au/internetexplorer/>.
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> erlang-questions mailing list
> erlang-q...@erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions
--
Johnny Billquist || "I'm on a bus
|| on a psychedelic trip
email: b...@softjar.se || Reading murder books
pdp is alive! || tryin' to stay hip" - B. Idol
Backslash is an escape character within string syntax, like in C.
So if you want your literal string to contain a backslash (you
do, in order to remove the special meaning '[' has in REs),
you need to write "\\" as you discovered. See section 2.14 of
Erlang Reference Manual for more details.
HTH,
-- Jachym
There are two levels of backslash escaping here, one for string literals
and one for regular expressions. The \\ in the string literal becomes \ in
the run-time string, which the regex implementation treats as an escape
character for the following [. If you only write \[ in the string literal
then this becomes [ in the run-time string which the regex implementation
treats as the start of a character class specifier, and since there's no
closing ] it throws a syntax error.
Tony.
--
f.anthony.n.finch <d...@dotat.at> http://dotat.at/
GERMAN BIGHT HUMBER: SOUTHWEST 5 TO 7. MODERATE OR ROUGH. SQUALLY SHOWERS.
MODERATE OR GOOD.