heyitsmyusername
heyitsmyusername

Reputation: 651

PHP/Regex: Remove everything up to first word with at least 2 letters but include numbers that are touching the start of the word

How do I remove everything up to first word with at least 2 letters, but include numbers that are touching the start of the 2+ letters? The below code currently removes everything up to the first letter.

$test = "1234 123423-34 b4 3-z a 234 This is a test";
echo preg_replace('/^[^A-Za-z]+/', '', $test);

Should output: This is a test

$test = "1234 123423-34 b4 3-z a 234This is a test";
echo preg_replace('/^[^A-Za-z]+/', '', $test);

Should output: 234This is a test

Upvotes: 0

Views: 33

Answers (2)

Jan
Jan

Reputation: 43169

Another one with word boundaries:

^.*?(?=\b\d*(?i)[a-z]{2,}\b)

See a demo on regex101.com.

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89547

You can use this pattern:

/^.*?(?=\d*[a-z]\d*[a-z]|$)/i
# ^  ^------ lookahead: followed by a "word" with 2 letters or the end of the string 
# '--------- any character + non-greedy quantifier: all characters until

demo

The same in unicode:

/.*?(?=\d*\pL\d*\pL|$)/Au

demo

Upvotes: 3

Related Questions