• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular Expression  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to write a regular expression in which I allow the user to enter 0-9, a-z, A-Z and / (back slash).
so i wrote:
^[A-Za-z0-9/]*$

The only problem is that I want to restrict the user to enter a row of slashes /////////

i.e the only allowed values are of the type:
/home/desktop/root

Can someone please suggest how should i change the exp to incorporate this situation.

Please Help!
 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing Swing related here.
Moving to more appropriate forum
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neh Agarwal wrote:Hi,
i.e the only allowed values are of the type:
/home/desktop/root

This should work:
^(/\w+)+$

If you want to include file extension like
/home/desktop/root/config.txt
then use
^(/\w+(\.\w+)*)+$
 
Neh Agarwal
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't work!

For simple inputs like /root/log also it shows as invalid.

Please suggest what chages should I make!

 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error do you have? and which one doesn't work? and you may have to escape the ' \' character.
 
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neh Agarwal wrote:This doesn't work!

That's useless , You have to post here, what you did, what you expected and what you got !!

Neh Agarwal wrote:
For simple inputs like /root/log also it shows as invalid.

Please suggest what changes should I make!



Interestingly, when I made a RE as Duc suggested, it doesn't get complied, stating "illegal escape character \w"

Here is code I tried,


What is the cause ?
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Rohankar wrote:
What is the cause ?


I found out the problem, Actually you have to escapee the escape character, like

"\\w+", this works !! Thanks Guys !!
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Rohankar wrote:
What is the cause ?


I found out the problem, Actually you have to escapee the escape character, like

"\\w+", this works !! Thanks Guys !!
 
Neh Agarwal
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have written, "^(\\w+)+$" as the regular expression, and now I am giving the value as /root/log

It is not matching, but I want this to be matched

Please Help!
 
Neh Agarwal
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the problem, I had to allow words and / so I changed it as:

^(/\\w+)+$

It works!

Thanks all!
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neh Agarwal wrote:I found the problem, I had to allow words and / so I changed it as:

^(/\\w+)+$

It works!

Thanks all!


Great to hear it works, anyway you are confusing between regular expression string and with how to put it to Java code to have that string value.

The code ^(/\\w+)+$ is a coding way of to ask Java to initialise a string with value ^(/\w+)+$.

If you talk about regular expression, it should be ^(/\w+)+$.

If you talk about coding then it should be as below because it's how you code.


Please don't confuse it when you talk about regular expression.

Cheers,

Duc
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duc Vo wrote:

The code ^(/\\w+)+$ is a coding way of to ask Java to initialise a string with value ^(/\w+)+$.

If you talk about regular expression, it should be ^(/\w+)+$.


hmm, great things to learn, thanks
 
Sheriff
Posts: 21136
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Unix, almost every character is allowed as a file or folder name, so you may want to replace the \\w by something else:
The [^/] means any character that is not /.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!