regex - search for pattern that starts with alphabetics and ends with alphabets or space

StackOverflow https://stackoverflow.com/questions/9741769

  •  24-05-2021
  •  | 
  •  

سؤال

What is the correct regex for getting a string that contains only letters, must start with letters and a continuous string of letters. But can end with letters OR a space (just space and not tabs or returns).

I have this pattern /^\S*[a-zA-Z]\s*$/

Is it correct? do I need the \S* at the start and how do I ensure that there is no spaces in between letters?

TIA

هل كانت مفيدة؟

المحلول

if it must start with letters, contain only letters (no spaces), and could end with a single space i think it should be

^[a-zA-Z]+ ?$

نصائح أخرى

^[a-zA-Z]+?[a-zA-Z ]$

This assumes a modern regular expression processor which allows for non-greedy (+?) repeats.

/^[a-zA-Z]+[a-zA-Z ]$/

No need for \S*

And you can test it online. There are websites such as http://www.solmetra.com/scripts/regex/ to check regex.

To be unicode compatible:

^\pL ?$
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top