Home > Archive > Microsoft SQL Server forum > September 2005 > Reg Exp for digits









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Reg Exp for digits
NickName

2005-09-18, 8:23 pm

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.

Don

Erland Sommarskog

2005-09-18, 8:23 pm

NickName (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

jussij@zeusedit.com

2005-09-18, 8:23 pm

> 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

NickName

2005-09-19, 3:24 am

Erland,

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

Don

NickName

2005-09-19, 3:24 am

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 Sommarskog

2005-09-19, 3:24 am

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

jussij@zeusedit.com

2005-09-19, 8:24 pm

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

NickName

2005-09-20, 3:24 am

Shoot, you're tricky, now that you say it's sort of generic, I tried it
and it works nice, thanks.

NickName

2005-09-20, 3:24 am

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 Li

Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com