-1

I am trying to parse a formula structure that I have created. So far It's really close. I just need some help with the regex to split the equal sign out.

Here's the string I'm testing

({1+1=2|2+2=4}+{1+2=3|2+3=5})=16+10

This is the output i'm currently getting

array(6) {
  [0]=>
  string(1) "("
  [1]=>
  string(13) "{1+1=2|2+2=4}"
  [2]=>
  string(1) "+"
  [3]=>
  string(13) "{1+2=3|2+3=5}"
  [4]=>
  string(1) ")"
  [5]=>
  string(6) "=16+10"
}

[EDIT] - Sorry forgot the expression.

$logical_test_parts = preg_split( "/({.+?})([\)|=|\+?])/" , $logical_test, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

the issue I'm having is on index 5.

I need index 5 to be "=" and index 6 to be 16+10

any help is greatly appreciated

joeb
  • 777
  • 8
  • 28

2 Answers2

4

Regex: [()=]|\{[^\}]+\}|[+-]|[^=]+$

By @anubhava you can reduce regex to: {[^}]+}|[()=+-]|[^=()]+

Details:

  • [] Match a single character present in the list
  • [^] Match a single character NOT present in the list
  • + Matches between one and unlimited times
  • $ Asserts position at the end of the string
  • | Or

PHP code:

$text = '({1+1=2|2+2=4}+{1+2=3|2+3=5})=16+10';
preg_match_all("/[()=]|\\{[^\}]+\\}|[+-]|[^=]+$/", $text, $matches);
print_r($matches[0]);

Output:

Array
(
    [0] => (
    [1] => {1+1=2|2+2=4}
    [2] => +
    [3] => {1+2=3|2+3=5}
    [4] => )
    [5] => =
    [6] => 16+10
)

Code demo

Srdjan M.
  • 3,310
  • 3
  • 13
  • 34
  • Nice expression. – Syscall Feb 01 '18 at 15:04
  • 1
    Not at all. Your expression seems to be better than mine :) [+1] – Syscall Feb 01 '18 at 15:07
  • 1
    Very good answer ++ use `print_r($matches[0]);` to reduce one level of array – anubhava Feb 01 '18 at 15:08
  • 1
    Also you can reduce this regex to: `/\{[^}]+}|[()=+-]|[^=()]+/` – anubhava Feb 01 '18 at 15:11
  • Thanks for the help guys. This regex works but I'm getting an issue on this string. `[1+1=2?10/2:0]`. its dropping the 1's off so I am left with `+=2` when you implode the array. – joeb Feb 02 '18 at 19:57
  • @joeb The regex in this post works only for the pattern `({}+{})=n`, the rest you need to figure out for your self or write a new post describing problems that you have with regex attempts... – Srdjan M. Feb 02 '18 at 20:18
0

If you want the equal signs removed, you should try this:

<?php

  $txt='({1+1=2|2+2=4}+{1+2=3|2+3=5})=16+10';

  $re1='.*?';   # Non-greedy match on filler
  $re2='(=)';   # Any Single Character 1
  $re3='.*?';   # Non-greedy match on filler
  $re4='(=)';   # Any Single Character 2
  $re5='.*?';   # Non-greedy match on filler
  $re6='(=)';   # Any Single Character 3
  $re7='.*?';   # Non-greedy match on filler
  $re8='(=)';   # Any Single Character 4
  $re9='.*?';   # Non-greedy match on filler
  $re10='(=)';  # Any Single Character 5

  if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6.$re7.$re8.$re9.$re10."/is", $txt, $matches))
  {
      $c1=$matches[1][0];
      $c2=$matches[2][0];
      $c3=$matches[3][0];
      $c4=$matches[4][0];
      $c5=$matches[5][0];
      print "($c1) ($c2) ($c3) ($c4) ($c5) \n";
  }

?>

Quick and dirty way of dealing with it. The code has been generated by https://txt2re.com/. It's a really good tool for RegEx

  • I dont want the equals sign gone, I just want it separated from the formula to the right – joeb Feb 01 '18 at 14:48