0

I have a optional fields in which I have to accept only single character and I should also accept empty string. Ex: if I have a middle name field, in which it accepts single char. but in future if he don't want any middle name on his account then I should persist empty to the middle name in the database.

I used the below regexp. but it is not working to accept empty string [^$]|[A-Za-z]+

Could some one help me on this?

1
  • 1
    + means 1 or more. ? means 0 or 1. Commented Sep 30, 2015 at 16:07

1 Answer 1

2

How about ^[A-Za-z]?$ (one or zero letters)?

Sign up to request clarification or add additional context in comments.

5 Comments

but can I know what is ? stands here
@Pradeep It means "match either 0 or 1 occurence of whatever came before it". In your case it means "match either zero or one character". Look here for more details (btw, if it is working for you, could you mark the answer as accepted - the checkmark on the left side?).
if I need to add + for the expression.How can I add that?
Could you please also tell me how if there is multiple characters with empty String acceptable rather than single character
@Pradeep regarding the comment about adding + to the expression - not sure exactly what you mean. What exactly do you need to match? And as for the last comment, yes, that's what * is for, so ^[A-Za-z]*$ will match either zero characters (empty String) or multiple letters (one or more)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.