Showing posts with label digits. Show all posts
Showing posts with label digits. Show all posts

Monday, March 26, 2012

Reg Exp for digits

Hi,

This is not SQL Server database problem, however, net search has failed
to generate a solution to the problem, I'm trying my luck at this NG
now.

Problem, remove the special character, [ from text.
e.g. text =
"this is [1] and [stuff] and some [2] and hat [3] and dog"

Desired result =
"this is 1 and [stuff] and some 2 and hat 3 and dog"

I know [[:digit:]] would find all the instances of digits inside
brackets such as [1], [2], [3]. However, I don't know how to get rid
of the special characters of [ and ].

Any regular expression expert out there?

Thanks.

DonNickName (dadada@.rock.com) writes:
> This is not SQL Server database problem, however, net search has failed
> to generate a solution to the problem, I'm trying my luck at this NG
> now.
> Problem, remove the special character, [ from text.
> e.g. text =
> "this is [1] and [stuff] and some [2] and hat [3] and dog"
> Desired result =
> "this is 1 and [stuff] and some 2 and hat 3 and dog"
> I know [[:digit:]] would find all the instances of digits inside
> brackets such as [1], [2], [3]. However, I don't know how to get rid
> of the special characters of [ and ].
> Any regular expression expert out there?

Since you did not specify which regular expressions you are using, I'm
taking the liberty to answer with the regexps I know, that is those of
Perl. In Perl you would say:

s/\[([0-9]+)\]/\1/g

The \[ means [, that is \ is an escape charactter. () is for grouping,
and \1 refers back to this paren.

I have only seen glimpses of the regexps in .Net, but it appears to be
similar to Perl. There might be different rules for when you need \
though. In Textpad that I use, I would have to say \( and \)to use parens
for grouping.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||> Problem, remove the special character, [ from text.
> e.g. text =
> "this is [1] and [stuff] and some [2] and hat [3] and dog"
> Desired result =
> "this is 1 and [stuff] and some 2 and hat 3 and dog"

Using the Zeus editor:

http://www.zeusedit.com

These are the search and replace regular expresions:

Search: (\[)([0-9]+)(\])
Replace: \2

Note: Zeus is shareware (45 day trial).

Jussi Jumppanen
Author: Zeus for Windows|||Jussi,

Thanks for the response, I think your tool seems clever, and yet I'm
trying not to introduce many tools within one project.

Don.|||Erland,

I appreciate your response, unfortunately I'm not using Perl.

Don|||NickName (dadada@.rock.com) writes:
> I appreciate your response, unfortunately I'm not using Perl.

If in you insist on not telling what you use, the responses may not always
be applicable.

Anyway, I hoped that you would be able to translate the Perl stuff to
whatever you are using.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||NickName wrote:

> Thanks for the response, I think your tool seems clever, and
> yet I'm trying not to introduce many tools within one project.

The good thing is the regexp's that I posted are just standard
unix style regexp's so they should work with any tool that can
handle these type of regexp's.

Jussi Jumppanen
Author: Zeus for Windows
http://www.zeusedit.com|||Shoot, you're tricky, now that you say it's sort of generic, I tried it
and it works nice, thanks.|||Erland,

Jussi's solution solved the problem. You seem a very kind person, if
one day I travel to Sweden and you're available, I would love to buy
you a beer.

Kind regards,

Don Lisql