How would regex look to handle this case? [closed]

-3

I can not separate words like this: In case I have CPFConsole , I wanted to separate to CPF and console.

The code I have separates like this:   ([A-Z][a-z]+)|([a-z]{0,})|([A-Z]{2,})

But this code separates this way: CPFC and onsele .

    
asked by anonymous 06.12.2016 / 21:13

3 answers

5

If the entry is CPFConsole and you want only CPF, you can use this expression:

(^[a-zA-Z]{3})+

This expression takes the first three letters of your word.

See working at OnlineRegex .

Edit: According to your comment, if I understand correctly, the expression that JuniorNunes has put up works:

^([A-Z]+)([A-Z]{1}[a-z]+)

See the OnlineRegex .

    
06.12.2016 / 21:23
3

Criteria (As I understand it)

  • Must separate words
  • To identify the separations and next word begins with a capital letter.
  • If all letters are upper case, do not separate. (CPF)
  • Subsequent letter to upper case is lowercase.

REGEX

Following the criteria we can set up REGEX like this:

pattern : /([A-Za-z])([A-Z][a-z])/g
replace : $1 $2

See working at REGEX101 .

Explanation

  • ([A-Za-z]) - Group 1, search for upper or lower case.
  • ([A-Z][a-z]) - Group 2, search for 1 uppercase letter, followed by 1 lower case letter.
07.12.2016 / 18:29
0

If you need to separate by counting the letters:

([A-Za-z]{3})([a-zA-z]*)

It will separate into two groups.

  

Full match 0-10 CPFConsole

     

Group 1. 0-3 CPF

     

Group 2. 3-10 Console

link

If you need something more specific:

(CPF)(Console)

link

    
07.12.2016 / 13:19