• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

How we can Validate pipe (|) sign using Java regex  RSS feed

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I want to validate letters, numbers, spaces and following characters _ - + * / ! # $ % ^ : ; () ~ , ‘ “
Given special characters are valid characters if above characters are present in my string then string is valid string otherwise that will be invalid.

Code is below.



but in this code I am unable to validate pipe |.

"[^A-Za-z0-9$+,:;/@#'-*()%!*_-~\"]" This is valid pattern | Pipe sign also not present in this pattern but still this code is showing pipe is present.

if any body have answer please reply.


Thanks
Roopesh




 
Greenhorn
Posts: 14
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the output you are getting? I ran your code, i am getting "There is no special char."

Thanks,
Pratik
 
Roopesh Rana
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes, Current output is "There is no special char." but output should be "There is a special character in my string "

Thanks,
Roopesh
 
Jain Pratik
Greenhorn
Posts: 14
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your regex is not correct, you need to escape all the special characters. \\ is used to escape characters. Put \\ before each special character.



Thanks,
Pratik
 
Roopesh Rana
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
space also valid character if I will give space in string it should return"There is no special char."
 
Jain Pratik
Greenhorn
Posts: 14
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Space is also a character, and your regex is not containing any provision for space(similar as pipe). So it will return There is a special character in my string.
Can you please explain what you actually trying to achieve?

Thanks,
Pratik
 
Roopesh Rana
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i want to validate name, if name contains letters, numbers, spaces and following characters _ - + * / ! # $ % ^ : ; () ~ , ‘ “
if above things are present in my string it is valid string otherwise invalid string


I want to achieve this validation.



 
Jain Pratik
Greenhorn
Posts: 14
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the regex i posted earlier is working as per your requirement. If anything else apart from characters mentioned in regex come to name, it will go into true state (There is a special character in my string).
 
Bartender
Posts: 1810
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roopesh Rana wrote:
i want to validate name, if name contains letters, numbers, spaces and following characters _ - + * / ! # $ % ^ : ; () ~ , ‘ “


Maybe this is a silly question, but that covers almost everything. Wouldn't it be easier to search for the invalid characters?
 
Marshal
Posts: 64493
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you escaping the comma? A comma is not a metacharacter.
 
Marshal
Posts: 24467
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

J. Kevin Robbins wrote:

Roopesh Rana wrote:
i want to validate name, if name contains letters, numbers, spaces and following characters _ - + * / ! # $ % ^ : ; () ~ , ‘ “


Maybe this is a silly question, but that covers almost everything. Wouldn't it be easier to search for the invalid characters?



Well, no, it's very far from covering everything. There are tens of thousands of characters in Unicode, and Java characters are Unicode characters. However the op says "I want to validate name"; it's a strange requirement which lets people have $ in their name but not é for example.
 
J. Kevin Robbins
Bartender
Posts: 1810
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Well, no, it's very far from covering everything. There are tens of thousands of characters in Unicode


So yeah, it was a silly question. I just looked at his requirements and looked at my keyboard and thought "what does that leave?" I didn't think about non-English alphabets and such.
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!