regex to match kbt-y102_9999_0001v-s001r and kbt-y102_999a - php

I'm looking for a Regex that converts strings like
kbt-y102_9999_0001v-s001v
into N1v-s1v
and
kbt-y102_999a
into N1a
kbt-y102_ => ignore everything until first underscore
9999 => N
_0001v => 1v
-s001v => -s1v
kbt-y102_9999_0001v-s001r => N1v-s1r
kbt-y102_9999_0002r-s001v => N2r-s1v
kbt-y102_9999_0001v => N1v
kbt-y102_9999_0002r => N2r
kbt-y102_999a => Na
kbt-y102_999aa => Naa
kbt-y102_9999a => Na
kbt-y102_9999aa => Naa
my attempt covers the first four cases: (.*)_[0-9]{4}_[0-9]{3}([0-9][vr])?((-s)0{0,2}+([0-9][vr]))? (regex fiddle)
But I'm struggling with 999a.

Following your patterns this is a general Regular Expression to extract required data:
^[^_]*_\d+([a-z]*)(?:_0*([1-9][a-z])(?:(-[a-z])0*([1-9][a-z]))?)?
It's long but has nothing more than some acceptable wildcards (tokens) in proper places. You need to replace match with:
N$1$2$3$4
Live demo

Related

simple regex to remove _ and 0

I'm looking for an regex that converts the following strings (string => result):
_0001 => 1
_0001r => 1r
_0021v-s001r => 21v-s1r
_0000_0001r => 1r
It should essentially remove the _ and all zeros.
My attempt is: /[^_0]/
but for some reason it doesn't work:
https://regex101.com/r/4CWo9S/3
Why bother with regex?
It's a simple str_replace that is needed.
$str = "_0001 => 1
_0001r => 1r
_0021v-s001r => 21v-s1r
_0000_0001r => 1r";
echo str_replace(["0","_"], "", $str);
output:
1 => 1
1r => 1r
21v-s1r => 21v-s1r
1r => 1r
https://3v4l.org/BrL1M
From your question I assume you mean /[_0]/ the ^ would negate the character class.
It's because you're negating the search with the ^ token. You need just to search for /[_0]/ and replace with "".

Regex to convert kbt-y102_9999_0001v-s001v => N1v-s1v

I'm looking for a Regex that converts a string like
kbt-y102_9999_0001v-s001v
into N1v-s1v
kbt-y102_ => ignore everything until first underscore
9999 => N
_0001v => 1v
-s001v => -s1v
kbt-y102_9999_0001v-s001r => N1v-s1r
kbt-y102_9999_0002r-s001v => N2r-s1v
my attempt: _(9{4})_?(.*)(-s)0+(\d) (regex fiddle)
You could capture 1v and 1r in a group and replace with:
N$1-S$2
[^_]+_[0-9]{4}_[0-9]{3}([0-9][vr])-s0+([0-9][vr])
Demo

Regex to match specific pattern

I am so bad at creating regex and I'm struggling with what I am SURE it's a simple stupid regex.
I am using PHP to do this match. Here is what I have until now.
Test string: 8848842356063003
if(!preg_match('/^[0-2]|[7-9]{16}/', $token)) {
return array('status' => 'failed', 'message' => "Invalid token", 'token' => '');
}
The regex must comply to this: Start with 0-2 or 7-9 and have EXACTLY 16 characters. What am I doing wrong? Because I get, as a match:
array(
0 => 8
)
And I should get:
array(
0 => 8848842356063003
)
By the way: I am using PHP Live Regex to test my regex string.
Thanks in advance,
Ares D.
The regex must comply to this: Start with 0-2 or 7-9 and have EXACTLY 16 characters
You can put starting numbers in same character class and use end anchor after matching 15 more charaters:
/^[0-27-9].{15}$/
If you want to match only digits then use:
/^[0-27-9]\d{15}$/

PHP Count Number of Times A String Appears As A Value Inside An Array

So I looked around here for a bit but cant seem to get this right, so I am forced to make a new post. I gave it an effort! Don't hurt me.
Sample Array:
$ndata = Array
(
[ARMOR_HEAD] => Andariel's Visage
[ARMOR_TORSO] => Blackthorne's Surcoat
[ARMOR_FEET] => Illusory Boots
[ARMOR_HANDS] => Gauntlets of Akkhan
[ARMOR_SHOULDERS] => Pauldrons of Akkhan
[ARMOR_LEGS] => Blackthorne's Jousting Mail
[ARMOR_BRACERS] => Nemesis Bracers
[ARMOR_MAINHAND] => Gyrfalcon's Foote
[ARMOR_OFFHAND] => Jekangbord
[ARMOR_WAIST] => Blackthorne's Notched Belt
[ARMOR_RRING] => Ring of Royal Grandeur
[ARMOR_LRING] => Stone of Jordan
[ARMOR_NECK] => Kymbo's Gold
)
$count = count(preg_grep('Akkhan', $ndata));
print_r($count);
So this is only returns 1 instead of 2. I also tried array_search(), but that simply returns a the first found with its key. Or in_array but that is just boolean I guess.. Is there a better way to go about this?
The first parameter to preg_grep is not a string, it is a regular expression pattern represented as a string. Try this:
preg_grep('/Akkhan/i', $ndata)
For more information, see the documentation page for preg_grep.

Regex Optional Matches

I'm trying to match two types of strings using the preg_match function in PHP which could be the following.
'_mything_to_newthing'
'_onething'
'_mything_to_newthing_and_some_stuff'
In the third one above, I only want the "mything" and "newthing" so everything that comes after the third part is just some optional text the user could add. Ideally out of the regex would come in the cases of above;
'mything', 'newthing'
'onething'
'mything', 'newthing'
The patterns should match a-zA-Z0-9 if possible :-)
My regex is terrible, so any help would be appreciated!
Thanks in advanced.
Assuming you're talking about _ deliminated text:
$regex = '/^_([a-zA-Z0-9]+)(|_to_([a-zA-Z0-9]+).*)$/';
$string = '_mything_to_newthing_and_some_stuff';
preg_match($regex, $string, $match);
$match = array(
0 => '_mything_to_newthing_and_some_stuff',
1 => 'mything',
2 => '_to_newthing_and_some_stuff',
3 => 'newthing',
);
As far as anything farther, please provide more details and better sample text/output
Edit: You could always just use explode:
$parts = explode('_', $string);
$parts = array(
0 => '',
1 => 'mything',
2 => 'to',
3 => 'newthing',
4 => 'and',
5 => 'some',
6 => 'stuff',
);
As long as the format is consistent, it should work well...

Categories