Small developer Small developer - 1 year ago 30
PHP Question

Regex isn't working properly with PHP or not getting how to implement this

There is something really I couldn't understand is how can I check my previous match with the next character and set starting and ending character please guys help me.

Here is an Example of my string

..A..B..A...B.A.B


What I'm trying to do is starting of string:

1=> Check the first character is
..
or
A


2=> and the Second thing is String cannot be like this
..A..A
it must be like
..A..B..
and sequence.

3=> Ending character must be
..
or
B
and won't be
A


However, I can match the first character like so
^([A]{1}|[.]{1,100})
But when I'm trying this same way with ending character it is not working and I'm not getting how to do the step
2
.

Save my day guys. Thanks

Failed Regex:
^[\.{1,40}|A{1}]+(?!A)+(B)+(?!B)+(B|\.{1,40})$

Answer Source

This regex should match the description you've given:

^(?:\.+?)?(A\.+?B\.?|\.\.)+$

^ is the start of the string (or line if m modifier is used).
(?:\.+?)? is one or more ., but it optional.
A\.+B\.? is looking for an A any amount of .s then a B and an optional ..
| is an alternative pattern we'll look at
\.\. are 2 .s
+ allows for the whole group to occur once or more $ is the end of the string (or line, again depends on modifier being used)

Demo: https://regex101.com/r/OUJxxc/3/ (Probably with a clearer description than I provided)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download