Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular Expression Question

 
sanat
Ranch Hand
Posts: 111
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"^[a-zA-Z]*$"
"^[A-Z]\\d[A-Z]\\d[A-Z]\\d$"
Can any one give me some sample examples about these regular expressions.
 
sasi kala
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sanat kumar,

As far as i know,

"^[a-zA-Z]*$" ---- means an expression starts with a,b,c...z or A,B,C..Z
example, a
Aa
AasdfZadsf


"^[A-Z]\\d[A-Z]\\d[A-Z]\\d$" --- means [A-Z] single char between A,B..Z
\\d means 0,1,...9 any on char

like that "B3E3S1". Note that here not * is given it mean only on char.

to represent "SLFDJLK3alsdkjf" or "KHDJKFHJK3" in regular expression use

^[A-Z]*\\d[a-z]*$ --- starts with zore or more capitals, there should be one digits after that zero or more small alphabets.

Hope this helps you.

All The Best
 
sanat
Ranch Hand
Posts: 111
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks sasi kala for your quick reply.
 
sanat
Ranch Hand
Posts: 111
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

What are the characters ^ and $.

Can anyone give a validation rule for email ids.

Thanks in advance.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I downloaded a tool called RegEx Coach. It has a free trial period but you may find you want to pay for it. When you highlight part of an expression it gives a plain English description. It's neat to work interactively with a tool like that while learning regular expressions.

One note: That particular tool is not 100% compatible with Java's regex. Plus you have to add escape characters in Java. Google for regular expression tools in Java for more. There is one that works as an Eclipse plugin if that sounds useful.
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Pattern class javadoc will help you with regex expressions.

^ means not
$ means end of line
 
Robert Lofthouse
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clear this up:

^ matches the beginning of the line, however, when its inside a character class (i.e. [^a-z]) it means 'not'.

Therefore: ^[a-zA-Z] would be mean 'match any uppercase or lowercase letter at the beginning of a line'.

$ matches the end of a line

* means 'match 0 or more occurances of the preceding character'.

Therefore:

^[a-zA-Z]*$ means: Match a string that starts with 0 or more characters that are either uppercase or lowercase. The string must consist fully of letters.

The following would match:

Robert
Ivor
Lofthouse

The following wouldn't match:

Robert3#
Ivo1r
2Lofthouse@
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic