Tengo las siguientes cadenas en las que necesito saber la posición de los datos por caracteres específicos.

Isaiah Kinney 06/2021 1 11111

Darius Knox 10/2020 1 11-334-555

Leo Wiley 07/2020 1 22-333

Muros de piedra 11/2020 2 112333

John Stone 12/2021 x xx-xx-xxx

Necesito saber en qué posiciones están los números marcados en negrita, cuando no tengo números en lugar de ellos, tengo x caracteres.

Intenté con esto "(\ d + (\ d + | - *) \ d +) (| $)" pero con esto tengo espacios en blanco antes del número en negrita.

-1
Xyloto 26 jul. 2020 a las 23:31

1 respuesta

La mejor respuesta

Uso

(?<!\S)(?:x+(?:-x+)*|\d+(?:-\d+)*)$

Ver prueba

EXPLICACIÓN

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
    \S                       non-whitespace (all but \n, \r, \t, \f,
                             and " ")
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    x+                       'x' (1 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      -                        '-'
--------------------------------------------------------------------------------
      x+                       'x' (1 or more times (matching the
                               most amount possible))
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      -                        '-'
--------------------------------------------------------------------------------
      \d+                      digits (0-9) (1 or more times
                               (matching the most amount possible))
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

El código generado por regex101 parece proporcionar los detalles esperados:

using System;
using System.Text.RegularExpressions;
 
public class Example
{
    public static void Main()
    {
        string pattern = @"(?<!\S)(?:x+(?:-x+)*|\d+(?:-\d+)*)$";
        string input = @"Isaiah Kinney 06/2021 111111
Darius Knox 10/2020 111-334-555
Leo Wiley 07/2020 122-333
Stone Walls 11/2020 2112333
John Stone 12/2021 xxx-xx-xxx";
        RegexOptions options = RegexOptions.Multiline;
 
        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

Salida:

'111111' found at index 22.
'111-334-555' found at index 49.
'122-333' found at index 79.
'2112333' found at index 107.
'xxx-xx-xxx' found at index 134.
4
Ryszard Czech 26 jul. 2020 a las 21:07