Reputation: 318
For example i have numbers text
8.40083897*
S903040058061*
51097653
1.51097653P
0051384034*51384034* 40083897* 40078422**
I need get from this
40083897*
40058061*
51097653
51097653
51384034*
40078422**
So i use regex
/([0-9*]{8,10})/
It work good except it takes and count numbers from left, how say to regex count from right?
So if exist * then start counting from asteriks and length only 9
So if exist ** then start counting from last asteriks and length only 10
So if not exist * then start counting from last digit and length only 8
Upvotes: 3
Views: 70
Reputation: 626738
You can use
\d{8}(?!\d)\*{0,2}(?!\*)
See the regex demo.
Details
\d{8}
- any eight digits(?!\d)
- no digit on the right is allowed\*{0,2}
- zero, one or two asterisks(?!\*)
- not followed with another asterisk.See the PHP demo:
$s = "8.40083897*\nS903040058061*\n51097653\n1.51097653P\n0051384034*51384034* 40083897* 40078422**\n1.51097653P";
if (preg_match_all('~\d{8}(?!\d)\*{0,2}(?!\*)~', $s, $matches)) {
print_r(array_unique($matches[0]));
}
Output:
Array
(
[0] => 40083897*
[1] => 40058061*
[2] => 51097653
[4] => 51384034*
[7] => 40078422**
)
Upvotes: 1