• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help with regex  RSS feed

 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a regex to help verifying email address:
1. at least 2 letter before @
2. at least 3 letter domain name
3. at least 2 letters for extension

I've never used regex before, please give example. thanks
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hendra Kurniawan wrote:I need a regex to help verifying email address:
1. at least 2 letter before @
2. at least 3 letter domain name
3. at least 2 letters for extension

I've never used regex before, please give example. thanks

Similar questions have been asked a number of times on here. A search should give you a few ideas which you can modify for your exact needs.
 
Patel Chintan
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hendra Kurniawan wrote:I need a regex to help verifying email address:
1. at least 2 letter before @
2. at least 3 letter domain name
3. at least 2 letters for extension

I've never used regex before, please give example. thanks


You may try like this -

[\w][\w]@[\w][\w][\w].[\w][\w]

this will take only word/char, you can append other thing in [] like to support digit you can add \d and so on.
 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Patel:
a1b@22a3bc.a2#b should be valid but regex said invalid. Your regex didn't work. any help?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have never used regex before, I would recommend starting with the tutorial....

http://docs.oracle.com/javase/tutorial/essential/regex/


As it is *not* a good idea to use something that you don't understand.... and for regular expressions, this is doubly so.

Henry
 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still haven't been able to figure it out. the "at least" part that makes it difficult.
at least 3 letters:
11a2b3c is valid
abc is valid
A112cb is valid

so you see, the 3 letters need not be consecutive. Do you know how to do this?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hendra Kurniawan wrote:still haven't been able to figure it out. the "at least" part that makes it difficult.
at least 3 letters:
11a2b3c is valid
abc is valid
A112cb is valid

so you see, the 3 letters need not be consecutive. Do you know how to do this?



Well, it is definitely not "difficult". However, it is not beginner easy either..... If you really want to attempt it, I would recommend, when going through the tutorial, to not attempt it til you learn the sections on "qualifiers" and "groups" first.

If you already know qualifiers and groups, and "still haven't been able to figure it out", then show us what you tried so far, and we can give you a hint in the right direction.

Henry
 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let's focus on something simpler : at least contains 3 letters:
possiblities that I can think of:
[a-zA-Z][a-zA-Z][a-zA-Z]
.*[a-zA-Z][a-zA-Z][a-zA-Z]
[a-zA-Z].*[a-zA-Z][a-zA-Z]
[a-zA-Z][a-zA-Z].*[a-zA-Z]
[a-zA-Z][a-zA-Z][a-zA-Z].*
.*[a-zA-Z].*[a-zA-Z][a-zA-Z]
.*[a-zA-Z][a-zA-Z].*[a-zA-Z]
.*[a-zA-Z][a-zA-Z][a-zA-Z].*
[a-zA-Z].*[a-zA-Z].*[a-zA-Z]
[a-zA-Z].*[a-zA-Z][a-zA-Z].*
[a-zA-Z][a-zA-Z].*[a-zA-Z].*
.*[a-zA-Z].*[a-zA-Z].*[a-zA-Z]
.*[a-zA-Z].*[a-zA-Z][a-zA-Z].*
.*[a-zA-Z][a-zA-Z].*[a-zA-Z].*
[a-zA-Z].*[a-zA-Z].*[a-zA-Z].*
.*[a-zA-Z].*[a-zA-Z].*[a-zA-Z].*

any hint? how to combine all those possibilities?
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you just SearchFirst for work that's already been done before rather than go through all the trouble again yourself? Sometimes, laziness can be a virtue: best email validation regex
 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont see any of those that conforms "at least".
? is for zero or one
* for zero or more
+ one or more

all are for consecutive. Or am I missing something here?

And I don't think I'm lazy. Right now I'm at office. I still work on Saturdays and Sundays. I just have lots of things to do:
http://www.coderanch.com/t/585469/Web-Services/java/file-web-service
http://www.coderanch.com/t/585766/JSP/java/async-JSP-servlet#2666374
broken XML application
inconsistent database data because another developer decided to manipulate data via management studio bypassing all business constraints, etc.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hendra Kurniawan wrote:I dont see any of those that conforms "at least".
? is for zero or one
* for zero or more
+ one or more

all are for consecutive. Or am I missing something here?



First, there is a qualifier that allows you to specify something like "three or more".

Second, as already mentioned, you can have a qualifier on a group -- so for example, if your group was "one letter surrounded by any amount of anything", then consecutive of that groups doesn't mean that the letters are consecutive.

which leads us to....

Hendra Kurniawan wrote:let's focus on something simpler : at least contains 3 letters:
possiblities that I can think of:
[a-zA-Z][a-zA-Z][a-zA-Z]
.*[a-zA-Z][a-zA-Z][a-zA-Z]
[a-zA-Z].*[a-zA-Z][a-zA-Z]
[a-zA-Z][a-zA-Z].*[a-zA-Z]
[a-zA-Z][a-zA-Z][a-zA-Z].*
.*[a-zA-Z].*[a-zA-Z][a-zA-Z]
.*[a-zA-Z][a-zA-Z].*[a-zA-Z]
.*[a-zA-Z][a-zA-Z][a-zA-Z].*
[a-zA-Z].*[a-zA-Z].*[a-zA-Z]
[a-zA-Z].*[a-zA-Z][a-zA-Z].*
[a-zA-Z][a-zA-Z].*[a-zA-Z].*
.*[a-zA-Z].*[a-zA-Z].*[a-zA-Z]
.*[a-zA-Z].*[a-zA-Z][a-zA-Z].*
.*[a-zA-Z][a-zA-Z].*[a-zA-Z].*
[a-zA-Z].*[a-zA-Z].*[a-zA-Z].*
.*[a-zA-Z].*[a-zA-Z].*[a-zA-Z].*

any hint? how to combine all those possibilities?


What about three or more of "a group formed by one letter surrounded by any amount of anything"? How would that look as a regex? And do you think it would work? .... in other words, you are almost there; you just need to do the easier regex mentioned, put that into a group, and then apply the three or more to it.

Henry
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use greedy quantifier: "X{n,}" means "X, at least n times". Here, you can take X as anything e.g. \w

http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried [a-zA-Z]{3,} for a1b2c3, but doesn't match.

@Henry Wong:
you are almost there; you just need to do the easier regex mentioned, put that into a group, and then apply the three or more to it


do you mean something like (.*[a-zA-Z].*){3,}? (without the question mark) gonna test it first.

so far so good. is this what you meant, any flaw? thanks

 
Siva Vulchi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Hendra Kurniawan
I tried [a-zA-Z]{3,} for a1b2c3, but doesn't match.


I guess you can solve the problem very easily if you spent some more time on understanding "Character Classes".
Small briefing about "Character Classes" : For every character present in the input string, the regex API validates against the character classes defined.

Your regex pattern "[a-zA-Z]{3,}" will only accept letters but not digits. So try adding digits range to the pattern.

Siva
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a big fan of The Regex Coach. It's a little utility that lets you quickly build and test regexes and see what exactly they match. I don't use it very often, but when I need to build an even slightly complicated regex, it is where I always start.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like the tutorial at http://www.regular-expressions.info/
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!