I need to build one RegEx to remove leading "The" or "A" or "An" and "spaces" from a given string.
For example, the given string is:
The quick brown fox jumps over the lazy dog
With Regex I want the leading "The" to be removed and return just
quick brown fox jumps over the lazy dog
I tried (added from a comment)
^*(?<=[The|An|A]\s){1}.*
It is working fine but in one scenario it is not returning expected result. Please see the scenarios below.
Input: The quick brown fox --> Result = quick brown fox
Input: A quick brown fox --> Result = quick brown fox
Input: In A sunny day --> Result = A sunny day (expected is In a sunny day.. as the string is not starting with A)
Input: American An bank --> Result = An bank (expected is American An bank.. as the string is not starting with An)
What have you tried by yourself? What you want to achieve is not difficult, try e.g. this tutorial on Regular-Expresions.info.
You are thinking much to complicated. Try this:
^(The|An|A)\s+
and replace with the empty string.
See it here on Regexr
^
matches the start of the string.
(The|An|A)
An alternation, matches the first fitting alternative.
\s+
matches at least one following whitespace.
Changes
The quick brown fox
A quick brown fox
In A sunny day
American An bank
To
quick brown fox
quick brown fox
In A sunny day
American An bank
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With