473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Regular Express Pattern Help!

We have a string like: 2004\05\05T00:00:00

We would like to remove all "\", any "-" and the T00:00:00 part of the
string.

The end result would be 20040505.

I know that if I use [\\-] as the pattern it will match on everything
except the final T00 part.

Could anyone please help us with this pattern.

TIA

Drew Mace
Jul 21 '05 #1
5 1696

I would just use a .Replace and SubString methods

So,

myString.Replace("\","").Replace("-","").SubString(0,8)
Drew wrote:
We have a string like: 2004\05\05T00:00:00

We would like to remove all "\", any "-" and the T00:00:00 part of the
string.

The end result would be 20040505.

I know that if I use [\\-] as the pattern it will match on everything
except the final T00 part.

Could anyone please help us with this pattern.

TIA

Drew Mace


--
w:4

Jul 21 '05 #2
Hi,

The following VBScript shows how you might make it work.

StringToSearch = "2004\5\05T00:00:00"
Set RegularExpressionObject = New RegExp
With RegularExpressionObject
.Pattern = "\\|T..:..:.."
.IgnoreCase = True
.Global = True
End With
MsgBox RegularExpressionObject.Replace( StringToSearch, "" )

Of course whether this will work for all your cases depends on how fixed
the structure of the input is.

Regards,

- Bruce.

"William Ewart Gladstone" <vi*******@era.politicos> wrote in message
news:13****************@news.west.earthlink.net...

I would just use a .Replace and SubString methods

So,

myString.Replace("\","").Replace("-","").SubString(0,8)
Drew wrote:
We have a string like: 2004\05\05T00:00:00

We would like to remove all "\", any "-" and the T00:00:00 part of the
string.

The end result would be 20040505.

I know that if I use [\\-] as the pattern it will match on everything
except the final T00 part.

Could anyone please help us with this pattern.

TIA

Drew Mace


--
w:4

Jul 21 '05 #3
To match the backslashes and the T... part, you can match "\\|-|(T.*)"
However, I'd suggest replacing "(\d\d\d\d)\\(\d\d)\\(\d\d)T\d\d:\d\d:\d\d"
with "$1$2$3".
Using this expression you can be sure you won't mess up data that's not
formatted as expected.

Niki

"Drew" <dr******@comcast.net> wrote in
news:e3**************************@posting.google.c om...
We have a string like: 2004\05\05T00:00:00

We would like to remove all "\", any "-" and the T00:00:00 part of the
string.

The end result would be 20040505.

I know that if I use [\\-] as the pattern it will match on everything
except the final T00 part.

Could anyone please help us with this pattern.

TIA

Drew Mace

Jul 21 '05 #4
It would be much easier to read your code if you didn't use regular
expressions. How about this idea:

string sourcestring = "2004\05\05T00:00:00";

string[] listbits = sourcestring.split('T');
string[] datebits = listbits[0].split('\');
string newdate = String.Join(datebits);

*caveat: air code... I didn't compile this in VS first.

That's pretty easy code, and it will work as long as the original data HAS a
"T" in it. (Fails otherwise).

Hope this helps,
--- N

"Drew" <dr******@comcast.net> wrote in message
news:e3**************************@posting.google.c om...
We have a string like: 2004\05\05T00:00:00

We would like to remove all "\", any "-" and the T00:00:00 part of the
string.

The end result would be 20040505.

I know that if I use [\\-] as the pattern it will match on everything
except the final T00 part.

Could anyone please help us with this pattern.

TIA

Drew Mace

Jul 21 '05 #5
I have to disagree with you: If you think 3 lines are more readable than 1
line, that's probably because you never learned regular expressions.

A few advantages of regexes:
- if you do understand them they are easier to read than c# code, because:
- they have no branches, exceptions, loops, etc, everything's
"straightforward" (quite literally)
- instead of specifying the transformation rules (which are pretty hards to
understand if you don't know what the source string will look like) you
specify the input pattern and the target output - that's high-level
programming;
- they can be compiled, and are often faster than a c# equivalent (make your
own timings if you don't trust me)
- they don't produce exceptions or endless loops due to bad/unexpected data
- they can easily be separated from the main program and extensively tested
(e.g. in Expresso)
- they don't have side-effects, i.e. the pattern can't mess around with any
global/local variables (yes, this is a BIG source for errors!)
- using regex'es it's far easier to create "picky" code, that doesn't
produce bad results from bad input; Your code e.g. would happily take
"Hello\\how\\are\\you\\Today" and transform it to "Hellohowareyou" - I
wouldn't want that in the "Date" column of my database...
(plus a few more good reasons I can't think of right now)

I agree with you that regex'es are often mis-used when "string.split" would
have done the same, but THIS kind of problem is what they have actually been
MADE for.

Niki
"Nick Malik" <ni*******@hotmail.nospam.com> wrote in
news:Tatzc.24279$Hg2.13887@attbi_s04...
It would be much easier to read your code if you didn't use regular
expressions. How about this idea:

string sourcestring = "2004\05\05T00:00:00";

string[] listbits = sourcestring.split('T');
string[] datebits = listbits[0].split('\');
string newdate = String.Join(datebits);

*caveat: air code... I didn't compile this in VS first.

That's pretty easy code, and it will work as long as the original data HAS a "T" in it. (Fails otherwise).

Hope this helps,
--- N

"Drew" <dr******@comcast.net> wrote in message
news:e3**************************@posting.google.c om...
We have a string like: 2004\05\05T00:00:00

We would like to remove all "\", any "-" and the T00:00:00 part of the
string.

The end result would be 20040505.

I know that if I use [\\-] as the pattern it will match on everything
except the final T00 part.

Could anyone please help us with this pattern.

TIA

Drew Mace


Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
5
by: Raj.SB | last post by:
I have a reg exp pattern as below works fine for the string : <param name="url" value="contents/cf_intro.htm" valuetype="ref"> mObjRegExp.pattern="<param.+?value=""(+)"" But the same is not...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
5
by: Craig Buchanan | last post by:
I need to strip the ()- and spaces from a telephone number. What is the regular express pattern for this? "\(\)-"? thanks
5
by: Drew | last post by:
We have a string like: 2004\05\05T00:00:00 We would like to remove all "\", any "-" and the T00:00:00 part of the string. The end result would be 20040505. I know that if I use as the...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
3
by: Chris | last post by:
Hi everyone, I'm trying to parse through the contents of some text files with regular expressions, but am new to regular expressions and how to use them in VB.net. I'm pretty sure that the...
0
by: peridian | last post by:
Hi, I wanted a web page where I could post code to, and have it appear in coloured formatting based on the context of the code. Most of the techniques I have seen for this involve complex use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.