How would I look regex to handle this case?

Asked

Viewed 210 times

-3

I’m not able to separate words this way: In case I have CPFConsole, wanted to separate to cpf and console.

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

But that code separates that way: CPFC and onsele.

  • What is the criterion for separating words?

  • See if that helps you: ^([A-Z]+)([A-Z]{1}[a-z]+)

  • Please post examples of how the original string looks and how you want it to look.

  • @Juniornunes your issue was rejected because you tried to add information that was not authored by the author of the post, and this is not allowed. Always choose to question the comments and instruct the author to make the edit, when additional information is required.

  • @diegofm http://answall.com/questions/170053/como-ficaria-regex-para-tratar-esse-caso?noredirect=1#comment350469_170055

  • @Juniornunes even so, the correct is to always instruct the author to edit and add, because in the queue you can’t read all the comments that are in the question and the answer, and this type of edition is usually always rejected.

Show 1 more comment

3 answers

5

If the entrance is CPFConsole and you want just CPF, you can use that expression:

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

This expression takes the first three letters of your word.

See working on Onlineregex.


Edit: According to your comment, if I understood correctly, the expression Juniornunes put works:

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

See on Onlineregex.

  • So, but it wouldn’t just be to get the number, it would be one of the test cases. It would have to pass in "ambientRG10", and leave each word separate, that regex I got, now when I have an equal word mentioned above "Cpfconsole", the idea in this case would only be to separate a group of capital letters up to the penultimate letter, before they become minute, I do not know if it was clear, this way he would separate (CPF, Console) and return me the two, the same is valid for a case like: "Rgcasa", would have to separate RG and Casa.

  • 3

    @Joseaguiar, these details must be given before you have an answer, precisely to avoid answers that will not help you. When you ask a question ask all relevant information.

  • 3

    @Taisbevalle you are correct in your comment, but it should also be noted that if there is any doubt, it is necessary to question the OP in the comments before answering, to avoid this kind of situation too, so that after the question is clear enough to get an answer, this be answered :)

  • 1

    But from what he explained now in the comment this answer is a solution to his problem!

  • 1

    Thank you very much!!

  • 1

    @Joseaguiar do not forget to mark the answer as a solution (on the left side of this post) so that other people are helped as well!

Show 1 more comment

3

Criteria (For what I understood)

  • Must separate words
  • To identify the separation and next word starts with uppercase.
  • If all letters are uppercase you should not separate. (CPF)
  • The letter after capital is minuscule.

REGEX

Following the criteria we can set up the REGEX like this:

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

See working on REGEX101.

Explanation

  • ([A-Za-z]) - Group 1, search 1 capital letter or minuscule.
  • ([A-Z][a-z]) - Group 2, search 1 uppercase letter, followed by 1 minuscule letter.
  • Simplified and improved, great example of regex101 +1

  • Thank you very much!!

0

If you need to separate by counting the letters:

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

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

  • Thank you very much!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.