aslamdoctor
aslamdoctor

Reputation: 3935

How to search set of specific pattern string using regular expression?

I want to search a set of specific string from a word using PHP regular expression. The pattern is like this

  1. The string is of 6 character length

  2. First 5 characters of this string are number 0-9

  3. Last 1 character is a A-Z or a-z alphabets

  4. there are more then one strings like this in a Word.

So eg. the word is

037165L_55084L_1959Z

Then the output of regular expression should return below 2 strings

37165L
55084L

Upvotes: 1

Views: 69

Answers (3)

anubhava
anubhava

Reputation: 785246

You can use just use this regex in preg_match_all function:

\d{5}[a-zA-Z]

RegEx Demo

Upvotes: 2

umka
umka

Reputation: 1655

This regex should work for you:

 /([0-9]{5}[A-Za-z]).*([0-9]{5}[A-Za-z])/

Upvotes: 0

Bruce
Bruce

Reputation: 1671

Use this function

    $string="your_character"
    $string = str_replace('your_character',' ',$string);
echo  $string;

Eg..

$string="037165L_55084L_1959Z"
    $string = str_replace('_55084L_1959Z',' ',$string);
echo  $string;

Upvotes: 0

Related Questions