Replace numbers using regular expressions

techiewithinme

New Member
Joined
Oct 4, 2011
Messages
7
Hello,

I have a date value in the below format and I want to replace all numbers after seconds part with blank. How do I do that using regular expression?

10/4/2011 01:10:11:000300000 AM

Thanks.
 
Following up on GTO's sugeestion of using a UDF.
If GTO is correct in his suggestion, then we don't need the heavy artillery of a RegExp engine to do what the OP asked for... this one-liner UDF should do what he wants:

Code:
Function ModifyTime(DateExp As Variant) As String
  ModifyTime = Left(DateExp, InStr(DateExp, ":") + 5) & Right(DateExp, 3)
End Function
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
If GTO is correct in his suggestion, then we don't need the heavy artillery of a RegExp engine to do what the OP asked for... this one-liner UDF should do what he wants:

Hello,

I have a date value in the below format and I want to replace all numbers after seconds part with blank. How do I do that using regular expression?

10/4/2011 01:10:11:000300000 AM

Thanks.

Thanks for your help. Is there any way to replace it by using regular expressions (find and replace functionality) instead of formulas?


I found another easy formula...
But I want with regular expression. Is there any good source info using regular expression in excel?

Hi Rick:)

While a RegExp may be a bit much for what is being done; well, call me silly, but I suspect the OP may actually want to try RegExp.;)

techwithinme,

Here are some links:
http://www.aivosto.com/vbtips/regex.html
http://msdn.microsoft.com/en-us/library/1400241x(VS.85).aspx
http://www.regular-expressions.info/tutorial.html
http://msdn.microsoft.com/en-us/library/ms974570.aspx

Hope that helps,

Mark
 
Upvote 0
Agreed that the OP asked for regex but it isn't clear if he meant using Find (which is a GUI dialog) or in a formula, if UDF's are ok, if he/she knew that other formulas could do the same job, or even if techie just wanted to know in what ways Excel supports regex. Unfortunately, both Find and Regex are terms that can have different contexts.

We will just have to wait and see if techie ever comes back as it's a moot point until then...

ξ

EDIT:
Though, Leith - I'm not familiar with this syntax in RegExp.Replace()
ModifyTime = re.Replace(DateExp, "$1 $2")
I'll look that up in the RegExp reference tonight unless you have time to explain it first ...
(I assume we are somehow dealing with the parenthetical groups in the regex pattern).
 
Last edited:
Upvote 0
Hello xenou,

You're correct about the parentheses. The string is parsed into 2 parts. The $1 and $2 represent the internal Submatches of the string. There go from left to right. The Replace function concatenates these submatches with a space between them if the pattern is found. If not then the original string is returned.
 
Upvote 0
Excellent. I'm not very familiar with submatches in regex patterns - this looks like a good case where they can be useful. Thanks.
 
Upvote 0
Techie:
I just want to advise you that only forum regulars are allowed to post links in their signatures - a link in the post body that is not directly related to the to the post content falls under the same rule. See rule 4 here (a board regular is a member for at least three months and at least 100 posts)

xenou

Edit regarding regex it's not included in Excel so far as formulas and search dialogues go, I'm afraid.

thanks for making it clear xenou.
 
Upvote 0
Hi Rick:)

While a RegExp may be a bit much for what is being done; well, call me silly, but I suspect the OP may actually want to try RegExp.;)
Yes, I'm sure you are right. The point I should have made with my message is that, in my experience at least, rarely is the machinery and overhead of a RegExp engine needed to do parsing in Excel... VB's built-in string functions, coupled with the Like operator, are almost always more than enough to do the job.
 
Upvote 0
Agreed that the OP asked for regex but it isn't clear if he meant using Find (which is a GUI dialog) or in a formula, if UDF's are ok, if he/she knew that other formulas could do the same job, or even if techie just wanted to know in what ways Excel supports regex. Unfortunately, both Find and Regex are terms that can have different contexts.

We will just have to wait and see if techie ever comes back as it's a moot point until then...

ξ

EDIT:
Though, Leith - I'm not familiar with this syntax in RegExp.Replace()
ModifyTime = re.Replace(DateExp, "$1 $2")
I'll look that up in the RegExp reference tonight unless you have time to explain it first ...
(I assume we are somehow dealing with the parenthetical groups in the regex pattern).

I was trying to replace thousands of date time values in one shot in Excel if it supports regular expressions as it supports in Word. Looks like there is no direct replacement otherthan using some functions. But anyways thanks for all your prompt inputs.
 
Upvote 0
Hello techiewithinme,

Following up on GTO's sugeestion of using a UDF. Here is an example with a Regular Expression.
Code:
Function ModifyTime(ByRef DateExp As Variant)

    Dim RegExp As Object
    
        Set RegExp = CreateObject("VBScript.RegExp")
        RegExp.Pattern = "(.+)\:\d{3,}\s(.+)"
        
        ModifyTime = RegExp.Replace(DateExp, "$1 $2")
        
End Function

How to use this function in excel?
 
Upvote 0
Code:
Function ModifyTime(ByRef DateExp As Variant)

    Dim RegExp As Object
    
        Set RegExp = CreateObject("VBScript.RegExp")
        RegExp.Pattern = "(.+)\:\d{3,}\s(.+)"
        
        ModifyTime = RegExp.Replace(DateExp, "$1 $2")
        
End Function

How to use this function in excel?
Once you install the code in a standard module (the same place you put macro code), then you just use the function like any other built-in Excel function. For example, if you had a text string like the OP posted in A1, then you would simply put this formula in the cell you wanted to display the result in...

=ModifyTime(A1)
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top