How to find-and-replace text with regular expression

I have multiple lines like this(with multiple random numbers):

value:"200 text.text.text.text
value:"235437 text.text.11.text
value:"12835 text.text.17.text
value:"347783 text.text.text.text

I want to remove everything before the space text.text.text.text

To look like this(i want to remove the space too):

text.text.text.text
text.text.11.text
text.text.17.text
text.text.text.text

2 Answers

In your case there are a couple of easy solutions. This first solution uses only basic regex:

[\.\w]+$

This captures every word character \w or period \. from the end $, stopping when it reaches any other type of character. This works because the space is not a word character or a period.

If you would like to select the region you want to remove, you can just use:

.*

Read this as match every character .* until the last space . The reason this matches until the last space, is because the star is a greedy quantifier. This means it matches everything it can, then starts giving back characters as needed for the rest of the pattern to match. To match until the first space, use .*? . The question mark next to the star makes the star lazy. This means the star will match as little as it has to in order for the next part of the expression to find a match.

However, capture groups are ideal for problems like this, where you would like to remove part of a line and keep the rest. A simple formula for this is:

^(.*)pattern_to_remove(.*)$

You can then recover the rest using backreferences. These store the capture groups (anything in parenthesis) from you regex into preset variables. In the pattern above, this would be \1\2 or $1$2 depending on the language you are using. This brings me to probably the easiest answer to your problem:

(.*)

The first capture group contains everything after the space. This regex is very straightforward and easy to read. Move to the first space , then capture everything after (.*).

For a comprehensive reference on regex, check out regular-expressions.info. Here is a link to the page on capture groups.

  • Ctrl+H
  • Find what: ^\S+\h+
  • Replace with: LEAVE EMPTY
  • CHECK Wrap around
  • CHECK Regular expression
  • Replace all

Explanation:

^ # beginning of line \S+ # 1 or more non spaces \h+ # 1 or more horizontal spaces

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like