Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a leading string using regular expression

Tags:

regex

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)

like image 794
Kiran Avatar asked Jan 14 '23 10:01

Kiran


1 Answers

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

like image 127
stema Avatar answered Feb 01 '23 14:02

stema