• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Regex for backslash

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am trying to have backslash(\) in my expression.Its an escape character but for its own use in an expression,I am unable to find a solution.I have tried "\\\\" but it doesnt accepts it.

 
Greenhorn
Posts: 5
Netbeans IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

\\ should match \, basically you are escaping the backslash itself.

If you need the expression as a string in code you will need to use "\\\\"
 
Marshal
Posts: 75839
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"\\" ???
 
Colin Brewster
Greenhorn
Posts: 5
Netbeans IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
\\ matches the backslash character, so for example if the expression is entered from a console that would match. However if the pattern is created from a string literal then you would need "\\\\" (double backslashes are required for a backslash in string literals).
 
prach Gupta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Colin,
I am trying the regex "\\\\" only but it doesnot seem to work.I have tried it many a times but it is taken as an invalid character.
 
Colin Brewster
Greenhorn
Posts: 5
Netbeans IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prach

Could you post some sample code showing your problem? "\\\\" works fine for me.

Thanks
 
prach Gupta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
My Requirement is ---Only the following characters are allowed in integer fields:
0 - 9, ( ) + <space> , - / \
and the regex that I have used is "^\\+?[0-9\\(?\\)\\s\\-\\,\\/]*" which works fine but if I add "\\\\" into it for validation of backslash ,invalid character error is thrown.
 
Colin Brewster
Greenhorn
Posts: 5
Netbeans IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I'm not sure why that isn't working for you, it looks fine to me although there are a few unnecessary escapes and a rogue "?" in the expression you posted, though for me they dont cause any exception to be thrown.

This is a slightly cleaner version of your expression that works fine for me: "^\\+?[-0-9]()\\s,-/\\\\]*"

If you just use "\\\\" on it's own do you still get the exception? Maybe it's environmental?
 
reply
    Bookmark Topic Watch Topic
  • New Topic