a regex expression

Vu Pham vu at sivell.com
Fri Nov 5 09:54:49 PDT 2010


  On 11/05/2010 09:45 AM, Vu Pham wrote:
>  I get several strings in the format "bla bla bla # something here 
> 78900". I want to get rid of anything inclusively from # to the last 
> space before the last 5 digits, so that
>
> bla bla bla # something here 78900    => bla bla bla 78900
> bla bla bla # 12345 something 77024 here 98765    => bla bla bla 98765
>
> The regex "\#.* [0-9]{5}$" gives me the condition I want but it also 
> removes the last 5 digits I want to retain.
>
> In other words, I would like to look for a "replace" regex that 
> removes anything from the first # to the end but the last 5 characters.
>
> Any advice is greatly appreciated.
>
> Vu

I found that, instead of using replace,  using capturing parentheses 
solves my problem. Something like ^([^#]*)(\#.* )([0-9]{5})$ allows me 
to split the string into three parts, the first is from the start to 
before the first #; the second is from the first # to the end but the 
last five characters, and the third is the last five characters. 
Concatenating the first and third parts gives me what I want.

Anyway, I would like to listen for more solutions.

Thanks,
Vu



More information about the Linux-users mailing list