aspose file tools*
The moose likes Struts and the fly likes Need help regarding with Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Need help regarding with "\n"" Watch "Need help regarding with "\n"" New topic
Author

Need help regarding with "\n"

Chethan Sharma
Ranch Hand

Joined: Mar 22, 2006
Posts: 59
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,


chaithu
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

If you know in Struts app the \n converts to \\n , then modify your Regular Expression like this :



Seems silly , but may help !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Chethan Sharma
Ranch Hand

Joined: Mar 22, 2006
Posts: 59
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

Joined: Mar 22, 2006
Posts: 59
"\n-" needs to allow for users on particular text box for enumeration purpose like bullets.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: Mar 22, 2006
Posts: 59
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
 
Don't get me started about those stupid light bulbs.
 
subject: Need help regarding with "\n"