vignesh
vignesh

Reputation: 1599

Need Regex to parse String

Using a regular expression, I want to parse a String like "DOCID = 1234567 THIS IS TEST" and remove the remaining String after the numbers.

How can I do this using the VIM editor?

Upvotes: 1

Views: 229

Answers (4)

Tassos
Tassos

Reputation: 3288

That will do the job:

:%s/\d\+\zs.*

Explanation:

  • % use the whole buffer, you can omit this if you want to change current line only
  • s the substitute command
  • \d\+ match as many numbers
  • \zs set the start of match here
  • .* everything else
  • you can omit the replacement string because you want to delete the match

Upvotes: 2

Arnaud F.
Arnaud F.

Reputation: 8452

In VIM, in command mode (press ESC), write :

:s/\([^0-9]\+[0-9]\+\).*/\1/

This will do the job. If you want to do all replacement possible, then :

:s/\([^0-9]\+[0-9]\+\).*/\1/g

Upvotes: 1

Hachi
Hachi

Reputation: 3289

in java string.replaceFirst("(= \\d+).*$","\\1");

Upvotes: 0

Toto
Toto

Reputation: 91385

In Perl:

$str =~ s/(= \d+).*$/$1/;

In php:

$str = preg_replace('/(= \d+).*$/', "$1", $str);

Upvotes: 4

Related Questions