foreach (string molecule in righ) { int index = molecule.IndexOf(elem); if (index == -1) buildArr.Add(-1); else { index += elem.Length; Match numberAfterElement = Regex.Match(molecule.Substring(index), @"^\d+"); if (numberAfterElement == null || !numberAfterElement.Success) { buildArr.Add(-1); } else { buildArr.Add(-1 * Int32.Parse(numberAfterElement.Value)); string test = ""; foreach (var z in buildArr) test += $"{z}, "; Debug.Log(test); } } } rrefArray.Add(buildArr);