• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Need help regarding with "\n"

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The application need to allow only printable characters, blank characters and \n-.
For this purpose I have written the below regular expression.

^(\p{Print}|\p{Blank}|\n-)*$

//****
exam = "this is for testing purpose message \n only";
regexp ="^(\p{Print}|\p{Blank}|\n-)*$";

public static boolean matchRegExp(final String exam, final String regexp) {
Pattern p = Pattern.compile(regexp);
Matcher m = p.matcher(exam);
return m.find();
}
//****

I have written this api in a java file (Test. java) and when I run Test. java as individual program it is working fine and it doesn't allow \n. I am getting the expected output.

But When I am calling the same api in my struts application, it is not working. It allows \n which is not the expected output.

When I am debugging the code using MyEclipse, in Variable view the message appears as

"this is for testing purpose message \\n only" ; ( via struts application)

"this is for testing purpose message \n only"; (individual program)


I am not sure why individual program is working fine but thru application it's not working.

Please let me know how to handle this "\n". Otherwise do I need to make any change in regular expression?


Thanks,
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you know in Struts app the \n converts to \\n , then modify your Regular Expression like this :



Seems silly , but may help !
 
Chethan Sharma
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.

I was on leave for the last few days. So I didn�t get chance to reply back.

Currently I am reading regular expression pattern from an xml file.

So I have defined the regular expression in xml file as:

^(\p{Print}|\p{Blank}|\n-)*$

My actual requirement is mentioned below

a) If I type explicitly "\n" and "\t" in the text box of my struts application it moves to next page, which means they were regarded as normal printable characters, but they shouldn't be allowed.
This is not the expected output. Here I am expecting the error message with out forwarding to next page.

b) However, if I use enter key and tab key which interprets to "\r\n" and "\t", it doesn't allow to next page. Getting error message and this is the expected output.


Please let me know how to define regular expression in an xml file to meet the above two scenarios.


Where as in java program we have to give the same regular expression with double slashes as

^(\\p{Print}|\\p{Blank}|\\n-)*$


But if I give with double slashes in xml file, during runtime expression I am getting the below exception

Exception: java.util.regex.PatternSyntaxException: Illegal repetition near index 5 "^(\\p{Print}|\\p{Blank}|\\n-)*$" ^

In xml file I tried with different ways but I didn�t get the expected output.

- ^(\p{Print}|\p{Blank}|\n-)*$
- ^(\\p{Print}|\\p{Blank}|\\n-)*$
- ^(\p{Print}|\p{Blank}|\\n-)*$

Thanks
 
Chethan Sharma
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"\n-" needs to allow for users on particular text box for enumeration purpose like bullets.
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chethan Sharma:


^(\p{Print}|\p{Blank}|\n-)*$

My actual requirement is mentioned below

a) If I type explicitly "\n" and "\t" in the text box of my struts application it moves to next page, which means they were regarded as normal printable characters, but they shouldn't be allowed.
This is not the expected output. Here I am expecting the error message with out forwarding to next page.


I`m just leaving so here is the quick reply ,

You want this characters , to be invalidated , '\n' and '\t' , So i suggest write this at start of RE , like ^(\n|\p{Print}|\p{Blank})*$ and If you want to invalidate then they should not be masked , i.e ignore them and allow the rest ! like ^([^\n]|\p{Print}|\p{Blank})*$

Originally posted by Chethan Sharma:

b) However, if I use enter key and tab key which interprets to "\r\n" and "\t", it doesn't allow to next page. Getting error message and this is the expected output.



May above logic helps !
 
Chethan Sharma
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sagar.

I didn't get the expected output after changed the regular expression as
^([^\n]|\p{Print}|\p{Blank})*$
in xml file

Thanks & Regards,
C
 
There's a way to do it better - find it. -Edison. A better tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic