get numbers after the last occurance of alphabet from string using php regex [closed]

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

  •  23-07-2023
  •  | 
  •  

Question

i have sequence of strings in excel file.

example:

1780CAR405108CCC72 

1780CAR405108KK89.0

1780CAR405108B7888

I need to get the numbers/floats whatever after the last occurrence of an ALPHABET like in these examples above, after CCC or KK or B. need assistance asap....

Was it helpful?

Solution

preg_match('#[^a-zA-Z]+$#', $string, $result);

OTHER TIPS

[a-zA-Z]([\d\.]+?)$

lazy match anchored at the end of the string, matches digits and fullstops into a capture group.

Also, since you're using PHP, you can use this if you don't want to deal with capture groups:

(?<=[^\d])[\d\.]+?$

You can match

[^a-zA-Z]+$

[^a-zA-Z] is a negated character class, it stands for "one character, any character but the ones inside the class". $ matches the end of the string.

See demo here.

Using preg_match:

preg_match('/[^a-zA-Z]+$/', "1780CAR405108B72", $match);
print($match);

$matches will contain numbers/float after alphabets.

preg_match('/[^a-zA-Z]+$/', "1780CAR405108B72",$matches);

'/pattern/'==> where slash is the delimiter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top