wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Reg Exp for @www.webinspect.hp.com Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reg Exp for @www.webinspect.hp.com" Watch "Reg Exp for @www.webinspect.hp.com" New topic
Author

Reg Exp for @www.webinspect.hp.com

Nitin Singla
Greenhorn

Joined: Oct 05, 2011
Posts: 15

Hi,


String str="@www.webinspect.hp.com";

I need to use a reg exp pattern for detecting @www.webinspect.hp.com for str.

Can anybody help me what should be the regexp for this type of string please?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11170
    
  16

What have you tried?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40


Another point is... if you are looklng for an exact string, you don't need regular expressions. There are methods of the string class that can check if a string contains a particular string value.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nitin Singla
Greenhorn

Joined: Oct 05, 2011
Posts: 15

hi
thanks for the reply..

str="@www.webinspect.hp.com";

is one example.. i just need one regexp for checking strings containing values like the one above in str..
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Nitin Singla wrote:is one example.. i just need one regexp for checking strings containing values like the one above in str.

Then you need to explain precisely what you mean by "values like". In fact, it'll be a big help in working out the regex for yourself.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Nitin Singla
Greenhorn

Joined: Oct 05, 2011
Posts: 15

Hi,

I mean to say str can be like

str="@****.com" -- where * denotes A-Z, a-z

examples:
str="@www.abc.com";
str="@www.webinspect.hp.com";
str="@www.def.com";
............


I am new to java and I am learning but i need this regexp. Please help.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11170
    
  16

We ARE helping. What we don't do is just give out an answer.

It is still not clear what you mean. I THINK you mean:

a literal '@' symbol

some number of alphabetics (what is the lower and upper limit?)

followed by the literal ".com"

However, your examples contradict what you state:

"@****.com" -- where * denotes A-Z, a-z

The, your example "@www.webinspect.hp.com" contradicts this. You say a '*' is a alphabetic, but this example has a '.' in it, between "webinspect" and "hp".

At the very least, you should give it a shot, and post what you try here.
Nitin Singla
Greenhorn

Joined: Oct 05, 2011
Posts: 15

Thanks for correcting me sir.
Let me write it very carefully this time:

Format for url:

str="@***.**.**.com"

Correct example is below:

str="@www.webinspect.hp.com";


I need a regexp for the above type of format. Can anybody help me out here?
Thanks in advance.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

So, tell us what you came up with after you have gone through some of the links found by this search: regex tutorial


Junilu - [How to Ask Questions] [How to Answer Questions]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Nitin Singla wrote:Format for url:

str="@***.**.**.com"

Aha. So @www.abc.com does not match? (Above, you said it should match). Because @www.abc.com does not have two dots before the ".com".

The point is, you need to exactly specify the rules. Once you've done that, you can formulate it as a regular expression. It's not good enough to say "it should be more or less like this". Computers only do exactly what you tell them to do - they can't work with approximate rules.

Have you already looked up the syntax for regular expressions in Java? So, what is your best guess for a regular expression for your case?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Michael Krimgen
Ranch Hand

Joined: Jul 08, 2012
Posts: 35
to help you to find the correct regular expression, I suggest you think that way:

"between the @ and the .com you need a string which consits of only characters (and numbers or special characters like _ ?) and dots. Furthermore, there should only be single dots
and no dot at the beginning or the end of the string".

However, there might be more conditions, depending on what exactly you want.

Hope that helps a bit!

Michael
Nitin Singla
Greenhorn

Joined: Oct 05, 2011
Posts: 15

I come up with this :

@([\w-]+\.)+[\w-]*

Please correct me if I am going in wrong direction?
But I am just asking for a help.. not for any humilation for beginners in java...

thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

Nitin Singla wrote:I come up with this :

@([\w-]+\.)+[\w-]*

Please correct me if I am going in wrong direction?
But I am just asking for a help.. not for any humilation for beginners in java...

thanks



There is a reason why we are being that exact, even to the point of being anal. And it is not for humiliation. The reason is, regular expressions needs to be exact. It is amazing how many situations are obvious to you, where it isn't to regular expressions.

Anyway, what happened when you ran that regex? Did it work for all test cases?

Henry
Michael Krimgen
Ranch Hand

Joined: Jul 08, 2012
Posts: 35
Nitin Singla wrote:I come up with this :

@([\w-]+\.)+[\w-]*

Please correct me if I am going in wrong direction?
But I am just asking for a help.. not for any humilation for beginners in java...

thanks


The [\w-]* seems to be redundand and you miss out the .com at the end?

@([\w-]+\.)+com
would be sufficient as the + sign means at least once.

Btw, aren't you missing the www. at the beginning?

Nitin Singla
Greenhorn

Joined: Oct 05, 2011
Posts: 15

Hi,

@www[\.]([\w-]+\.)+com

is updated one.

Is there any suggestions?

Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

Are you trying to validate an email address?
Apache commons validator is pretty good for it.


Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11170
    
  16

Nitin Singla wrote:
@www[\.]([\w-]+\.)+com

Is there any suggestions?

I have two suggestions:

1) TRY IT OUT against your test cases
2) tell us if it worked or not. If it doesn't work, tell us which cases don't work, and why you think it should.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Aniruddh Joshi wrote:Are you trying to validate an email address?
Apache commons validator is pretty good for it.

Also, as was pointed out to me by Rob Spoor: javax.mail.InternetAddress.

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Nitin Singla wrote:@www[\.]([\w-]+\.)+com
is updated one.
Is there any suggestions?

Yes. Stop coding.

You're suffering from "beginnersitis" at the moment, by which I mean that you're trying to solve everything with Java code.
There is no point in simply bashing out possible regexes and asking "is this right?". The fact is, if you deal with this properly, you'll know when it's right.

You've plainly done a bit of research into regexes, so you know what they expect; so my advice:
1. TURN YOUR COMPUTER OFF.
2. Sit down with your "pseudo-pattern" of '@***.**.*****.com' and write down the exact rules for it. You have an idea what regexes expect, so do it in those terms if you want; but write it in English, not it 'regex'ese.
3. When (and only when), you've done that, turn your computer back on and test it.

I'm afraid there's no shortcut to good code. You have to understand a problem before you can solve it.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reg Exp for @www.webinspect.hp.com
 
Similar Threads
Reg focus events
Reg. Validatin JTextFields
REG: Applet Database Connectivity
Help with Java Reg expr
reg audioclip