Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Help with Regexp, please

On Sat, 1 May 2010 20:19:07 -0400, "Rick Rothstein"
wrote:

Okay, I wasn't entirely sure what the "15,16" meant in the pattern. If the
numbers are always followed by a space (is that what the "\s\S" part of your
expression is for?), my code could be modified to this...


Whatever it is that follows the string of digits is undefined in the OP's
specifications. It could be a space; it could be another digit; it could be an
alpha character; it could be nothing. That is a problem with the
specification.

Also, whatever it is that precedes the string of digits is ALSO unspecified. It
could even be another digit!

If, as in the OP's example, the string of digits is ALWAYS preceded and
followed by a space, then his regex should have been something like:

\s\d{15,16}\s

If he only wanted to capture the standalone string of digits, then

\s(\d{15,16})\s would capture just the digits into Group 1.

and, expanding on that,

\s\d{15,16}\s+([\s\S]*) would also
capture everything after the string of digits into group 2 except for
the leading <space's before group 2.

In my suggestion, the [\s\S] will match every character that is either a
<space or not a <space. In other words, it captures everything.

If all I wanted to do was return everything in the string that came after a 15
or 16 digit number, that was bounded by spaces, I would just replace the
beginning of the string with nothing. It's much simpler, and probably faster.
===================================
Option Explicit
Function Part2(s As String) As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Pattern = "^[\s\S]+\s\d{15,16}\s+"
Part2 = re.Replace(s, "")
End Function
====================================

If I wanted to include the space prior to " Raffles Traders" as the OP did in
his example, then perhaps I would use this pattern:

re.Pattern = "^[\s\S]+\s\d{15,16}\b"

Now this would return the unaltered string if there was no match, but we could
easily test for that, depending on what the OP wanted to do in that instance.

-----------------
if re.test(s) = true then
part2 = re.replace(s,"")
else
part2 = "no pattern match"
end if
--------------------
--ron
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 150
Default Help with Regexp, please

Thanks for the solved problem and the learning about submatches that I
was not aware of.

Regards,
Raj




On May 2, 5:58*am, Ron Rosenfeld wrote:
On Sat, 1 May 2010 20:19:07 -0400, "Rick Rothstein"

wrote:
Okay, I wasn't entirely sure what the "15,16" meant in the pattern. If the
numbers are always followed by a space (is that what the "\s\S" part of your
expression is for?), my code could be modified to this...


Whatever it is that follows the string of digits is undefined in the OP's
specifications. *It could be a space; it could be another digit; it could be an
alpha character; it could be nothing. *That is a problem with the
specification.

Also, whatever it is that precedes the string of digits is ALSO unspecified. It
could even be another digit!

If, as in the OP's example, the string of digits is ALWAYS preceded and
followed by a space, then his regex should have been something like:

\s\d{15,16}\s

If he only wanted to capture the standalone string of digits, then

\s(\d{15,16})\s *would capture just the digits into Group 1.

and, expanding on that,

\s\d{15,16}\s+([\s\S]*) *would also
* * * * capture everything after the string of digits into group 2 except for
the leading <space's before group 2.

In my suggestion, the [\s\S] will match every character that is either a
<space or not a <space. *In other words, it captures everything.

If all I wanted to do was return everything in the string that came after a 15
or 16 digit number, that was bounded by spaces, I would just replace the
beginning of the string with nothing. *It's much simpler, and probably faster.
===================================
Option Explicit
Function Part2(s As String) As String
*Dim re As Object
Set re = CreateObject("vbscript.regexp")
*re.Pattern = "^[\s\S]+\s\d{15,16}\s+"
*Part2 = re.Replace(s, "")
End Function
====================================

If I wanted to include the space prior to " Raffles Traders" as the OP did in
his example, then perhaps I would use this pattern:

re.Pattern = "^[\s\S]+\s\d{15,16}\b"

Now this would return the unaltered string if there was no match, but we could
easily test for that, depending on what the OP wanted to do in that instance.

-----------------
if re.test(s) = true then
* * * * part2 = re.replace(s,"")
else
* * * * part2 = "no pattern match"
end if
--------------------
--ron


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Help with Regexp, please

On Sun, 2 May 2010 07:52:40 -0700 (PDT), Raj wrote:

Thanks for the solved problem and the learning about submatches that I
was not aware of.

Regards,
Raj


Glad to help. Thanks for the feedback.

In your initial posting, you had your regex within parentheses:

(\d{15,16})
^ ^

That captures that result into a capturing group, which, in VBA, is referenced
as a submatch. That was why I assumed that you were aware of that concept. I
should have been more explicit.
--ron
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
RegExp not matching what I want Phil Hibbs Excel Programming 9 April 21st 10 03:47 PM
RegExp & VB Noob Qs Ron Rosenfeld Excel Programming 2 June 28th 08 12:47 AM
RegExp Arshavir Grigorian Excel Programming 2 March 13th 08 12:55 AM
RegExp information Alain R. Excel Programming 1 February 18th 08 02:56 PM
RegExp - String in VBA MSK Excel Programming 0 July 24th 07 02:50 PM


All times are GMT +1. The time now is 08:24 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"