Regex only at first occurrence

Asked

Viewed 1,127 times

1

I’m wearing the sublime and would like to do a mass replace. My texts are in this pattern:

ABC ("Teste regex

I would like to remove the space after the ABC and it should stay that way:

ABC("Teste regex

How the regex would look ?

2 answers

1


It’s quite simple.

In the search use the following regex: (ABC)\s.

Where it is in parentheses is the content that will remain and the rest outside the group is what will be deleted.

In content to replace, use $1.

So basically we’re taking everything that was found in the regex "ABC " and replacing it with the contents of the first regex group that is "ABC".

$1 indicates that you are picking up everything found in the first regex group, parentheses define a group.

See working on Regexr

Recalling that the \s handles spaces, line breaks, tabs, etc. If you want to take only the space, you can put a blank at the end.

  • Can you illustrate ? That part of the $1 I can’t get.

  • @Douglas Editei, see if it helps.

  • Thank you.... It worked for me like this...: ([A-Z]{3})\s

  • @Douglas will also work, but you will get any content with 3 letters followed by a space. I did to pick up exactly ABC. Vlw

  • 1

    Boy, your explanation was genius.. I was confused with this group story...so they get in parentheses....

0

If your intention is to remove one or more spaces before a parenthesis you can do so:

inserir a descrição da imagem aqui

  • s serves to capture spaces, tabs, line breaks, etc.
  • The * immediately after means that this space may occur either several times.
  • And finally ( represents the opening of the parentheses, but as this is a special character for the regex we have to add the bar before to escape.

    Find: s*(

    Replace: (

Asism is possible to pick up various types of situation: Screenshot resultado

Browser other questions tagged

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