Home > other >  How can I make two names be understood with regex?
How can I make two names be understood with regex?

Time:06-01

I am having a bit of a problem with regex. I would like a regex that helps me to match any name (both single names or compound names with more than one word) which are completely capitalized and appear before a colon. For instance:

MARÍA: ¿Qué hago?
EL SEÑOR BENITO: Marcharte.

I would like a regex which is able to take both MARÍA and EL SEÑOR BENITO together with the colon, and remove this part.

I have tried this regex, but it only works for single names:

^[A-ZÁÉÍÓÚÜ] :

Any help would be much appreciated.

CodePudding user response:

The character class you have there does not allow spaces. Add a space to it:

^[A-ZÑÁÉÍÓÚÜ ] :

(Edited to add the Ñ that it needs)

CodePudding user response:

Use

^[\p{Lu}\s] :

See regex proof.

EXPLANATION

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  [\p{Lu}\s]               any character of: Unicode uppercase letter (\p{Lu}),
                           whitespace (\n, \r, \t, \f, and " ") 
                           (1 or more times (matching the most amount possible))
--------------------------------------------------------------------------------
  :                        ':'</pre>

If it does not work use

^[^:\r\n] :

EXPLANATION

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  [^:\r\n]                 any character except: ':', '\r' (carriage
                           return), '\n' (newline) (1 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  :                        ':'
  • Related