A
A
Alexander2020-04-29 11:04:57
PHP
Alexander, 2020-04-29 11:04:57

How to skip a character in a regular expression?

Hello everyone, I need from:

Data

B43.C21.D10.E110
B42.C11.D10.E120
B42.C11.D20
B42.C11.D20
B46.C71.D11.E000
B81.C21.D10.E000
B19.C20.D2
B81.C10.D10.E000
B80.C20.D10.E000
B46.C3
B36.C00.D11.E000

It is necessary to get a selection from this data, 2 digits dot 2 digits, for example, for the first line it is:
43.21
for the last and penultimate:
46.3
36.00

I did this https://regex101.com/r/aVoTvs/1, I can’t continue understand how to do

Answer the question

In order to leave comments, you need to log in

3 answer(s)
0
0xD34F, 2020-04-29
@0xD34F

First, cut out the excess from the line, then look for what you need:

preg_match('~\d{2}\.\d{1,2}~', preg_replace('~[^\d.]~', '', $str), $match);

W
Wynell_ru, 2020-04-29
@Wynell_ru

As far as I know, you cannot skip a character in a group. (But I could be wrong)
I suggest applying /^\w(\d{1,})\.\w(\d{0,2})/ - https://regex101.com/r/aVoTvs/5
And then just connect the values ​​of these groups through a dot

K
Karpion, 2020-05-03
@Karpion

I see that the original expression is expressed as:
letter_B digits dot letter_C digits {hereinafter optional} letter_D digits dot letter_E digits
Questions immediately arise:

  1. Could there be others instead of these letters?
  2. What should I do if there are many numbers after the letters B and / or C?

I see such a solution: Here, if there are a lot of digits, the first two are taken.
s/B(\d{1,2})\d*\.C(\d{1,2})\d*\..*/\1.\2/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question