Hi friends,
In general ruby ,backslash will be escape special chars after that.
example
" *importantdata " it will return " *importantdata "
Here i want to avoid escape chars after slash.slash also should
considered as like other special chars.But onething I dontwant to
replace slash with anything
so what’s your ideal syntax here?
Roger P. wrote in post #1056378:
so what’s your ideal syntax here?
i want to return string whatever i have given without escaping chars
followed by slash.So can we do anything that slash also consider as a
char like * in my example
On 04/13/2012 09:01 PM, Roger P. wrote:
so what’s your ideal syntax here?
if you need a avoid escaping completely you can use
/heredoc/
myText =<<DOC
Please Det \ \t \n \r ach and return this coupon with your payment.
Do not send cash or coins.
Please write your name and account number on the check and
make checks payable to:
Acme Corporation
Thank you for your business.
DOC
all characters will be saved as you print them without escaping
On 04/13/2012 09:01 PM, Roger P. wrote:
so what’s your ideal syntax here?
if you need a avoid escaping completely you can use
/heredoc/
myText =<<DOC
Please Det \ \t \n \r ach and return this coupon with your payment.
Do not send cash or coins.
Please write your name and account number on the check and
make checks payable to:
Acme Corporation
Thank you for your business.
DOC
all characters will be saved as you print them without escaping
" \imp “.match(/
[|@|#|$|+|{|}|’|”|?|<|>|*|-|!|%|^|&|:|;||=|.|~|`|^|]|[|’|\|/||]imp
/)
when i am trying to match string around only one special char.it is
returning matching bcz the reason escaping slash .
" $imp “.match(/
[|@|#|$|+|{|}|’|”|?|<|>||-|!|%|^|&|:|;||=|.|~|`|^|]|[|’|\|/||]imp
/)
it will not match .this is returned as i expected
In the firstcase also have 2 specialchars i want to expect that should
be work as like 2nd one
Vladimir wrote in post #1056383:
On 04/13/2012 09:01 PM, Roger P. wrote:
so what’s your ideal syntax here?
if you need a avoid escaping completely you can use
/heredoc/
myText =<<DOC
Please Det \ \t \n \r ach and return this coupon with your payment.
Do not send cash or coins.
Please write your name and account number on the check and
make checks payable to:
Acme Corporation
Thank you for your business.
DOC
all characters will be saved as you print them without escaping
Well, you cannot change the fact that backslashes are escape characters
in string literals (unless you modified the Ruby Parser). You either
have to double them or use the heredoc syntax mentioned by Vladimir.
But does this situation even occur? I mean: In you final program, you
probably won’t type in the strings by hand but rather read them from a
file or whatever. In this case, there’s no problem at all. A backslash
in a given string is a literal backslash. The escaping only happens with
string literals written in Ruby.
By the way: Your regex doesn’t work anyway, because you’re only checking
for substrings. The “false alarm” has nothing to do with the double
backslash:
" ****imp" => this also matches, because it contains the substring
“*img”.
Also the “|” are wrong in a character class. Why don’t you use the regex
we proposed in your other threads?
Hm. I know you can escsape some characters with URI::encode URI::decode
this methods for URLs but you can specify additional parameter - regexp
which select what symbols
you want encode. I’m not write custom regexp for thats.
But maybe this can help.
Now am using regular expression
" *\imp “.match(/
[|@|#|$|+|{|}|’|”|?|<|>|*|-|!|%|^|&|:|;||=|.|~|`|^|]|[|’|\|/||]imp
/)
It is satisfying coniditions which i was expected .It will not return
substring match except around specialchar.Am expecting to allow word
only when it has single specialchar around that.But this is failing when
string contains \
Jan E. wrote in post #1056404:
Well, you cannot change the fact that backslashes are escape characters
in string literals (unless you modified the Ruby Parser). You either
have to double them or use the heredoc syntax mentioned by Vladimir.
But does this situation even occur? I mean: In you final program, you
probably won’t type in the strings by hand but rather read them from a
file or whatever. In this case, there’s no problem at all. A backslash
in a given string is a literal backslash. The escaping only happens with
string literals written in Ruby.
By the way: Your regex doesn’t work anyway, because you’re only checking
for substrings. The “false alarm” has nothing to do with the double
backslash:
" ****imp" => this also matches, because it contains the substring
“*img”.
Also the “|” are wrong in a character class. Why don’t you use the regex
we proposed in your other threads?
On Fri, Apr 13, 2012 at 8:21 PM, Lucky Nl [email protected] wrote:
Roger P. wrote in post #1056378:
so what’s your ideal syntax here?
i want to return string whatever i have given without escaping chars
followed by slash.So can we do anything that slash also consider as a
char like * in my example
Escape the backslash:
irb(main):001:0> puts ‘a\b’
a\b
Kind regards
robert
Lucky Nl wrote in post #1056566:
Now am using regular expression
" *\imp “.match(/
[|@|#|$|+|{|}|'|”|?|<|>|*|-|!|%|^|&|:|;||=|.|~|`|^|]|[|'|\|/||]imp
/)
It is satisfying coniditions which i was expected .
No, it doesn’t. I’ve already said this in the answer you’re are quoting.
And I’ve already given you correct regexes which do what you want.
However, you don’t seem to care about our solutions (despite asking for
them again and again). I find this a bit annoying.
Test your regex with the string “abcimpxyz”. This matches for
the substring “*imp”, although it definitely shouldn’t. That’s because
you’re not checking the surroundings of the string. Instead, you’re
searching for any substring consisting of a “special char” and the
word “imp”.
Also note that the regex is too cumbersome, and the “|” inside the
character class don’t make any sense.
Since I don’t want to repeat everything I already said in your other
threads, here are the links:
http://www.ruby-forum.com/topic/3999160#1055090
(a solution for the “special chars”)
http://www.ruby-forum.com/topic/4038951#1055987
(a complete solution for the string matching)
Am expecting to allow word
only when it has single specialchar around that.But this is failing when
string contains \
No, that has nothing to do with backslashes, it’s a problem of your
regex (see above). But even if the backslashes were the problem: What
do you want to do about it? Go on complaining about it forever? We’ve
already given you solutions to the problem.
Please, read the answers you get. Then you don’t have to ask the same
questions again and again.
Robert K. wrote in post #1056724:
On Fri, Apr 13, 2012 at 8:21 PM, Lucky Nl [email protected] wrote:
Roger P. wrote in post #1056378:
so what’s your ideal syntax here?
i want to return string whatever i have given without escaping chars
followed by slash.So can we do anything that slash also consider as a
char like * in my example
Escape the backslash:
irb(main):001:0> puts ‘a\b’
a\b
Well, the problem is he doesn’t want to. He rather wants the backslash
not to be an escape character (but it seems he doesn’t want to use the
heredoc syntax either).
I’d say this is an impossible request.
Even if you did make the backslash a normal character (by messing with
the parser or whatever), there would still have to be an escape syntax
– with the exact same “problems” like the backslash.
On Mon, Apr 16, 2012 at 5:57 PM, Jan E. [email protected] wrote:
irb(main):001:0> puts ‘a\b’
a\b
Well, the problem is he doesn’t want to. He rather wants the backslash
not to be an escape character (but it seems he doesn’t want to use the
heredoc syntax either).
I’d say this is an impossible request.
Absolutely. Thats’s why I reinforced the proper solution.
Even if you did make the backslash a normal character (by messing with
the parser or whatever), there would still have to be an escape syntax
– with the exact same “problems” like the backslash.
Exactly.
Kind regards
robert