Re: Find and Replace - Changing 1st # only



On Thu, 11 Oct 2007 15:16:01 -0700, Studebaker
<Studebaker@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Hello,

I just want to change the 1st # in a column of 9 digit #s. Ex: I want to
find 712015647 and replace it with 612015647. I thought about finding
7??????? and replacing with 6???????? but it replaces my # with "6????????".
I want to keep the last 8 #s and just replace the 1st number from a 7 to a 6.
Can someone help?

Thank you,
Studebaker

If you enter the VBA Function below, you can use various patterns to handle
both the replacement in this thread, as well as in your other thread.

To enter the UDF, <alt-F11> opens the VB Editor. Ensure your project is
highlighted in the project explorer window, then Insert/Module and paste the
code below into the window that opens.

Now I'm not sure what you want to do in the example above, but if you wanted to
find any 9 digit number that started with a 7, and replace the 7 with a 6, you
would use the following:

=rerepl(A1,"7(\d{8})","6$1")

The expression: 7(\d{8})

means:

Match the character ?7? literally «7»
Match the regular expression below and capture its match into backreference
number 1 «(\d{8})»
Match a single digit 0..9 «\d{8}»
Exactly 8 times «{8}»

The expression "6$1"

means to return a 6, followed by returning backreference #1 (which was where we
captured the other 8 digits).

For your second issue, to remove the Dollar value, you can use the same UDF but
with different arguments. Although there are other ways to do it, as long as
you have the UDF, you might as well use it.

=TRIM(rerepl(A2,"\$.*?\s"))

Here the expression

\$.*?\s

means:

Match the character ?$? literally «\$»
Match any single character that is not a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as
needed (lazy) «*?»
Match a single character that is a ?whitespace character? (spaces, tabs, line
breaks, etc.) «\s»

So the match is for everything starting with the $ and ending with the next
<space>. We replace it with <nothing>.

Then the TRIM function removes any extra spaces.

============================================
Option Explicit
Function ReRepl(str As String, sPat As String, _
Optional sRepl As String = "") As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.ignorecase = True
re.Pattern = sPat
If re.test(str) = True Then
ReRepl = re.Replace(str, sRepl)
End If
End Function
=====================================
--ron
.



Relevant Pages

  • [TOMOYO #15 3/8] Common functions for TOMOYO Linux.
    ... This file contains common functions (e.g. policy I/O, pattern matching). ... Since TOMOYO Linux is a name based access control, ... TOMOYO Linux's string manipulation functions make reviewers feel crazy, ... the Linux kernel accepts all characters but NUL character ...
    (Linux-Kernel)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • Re: RfD: Escaped Strings
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... \b BS (backspace, ASCII 8) ... \ ** escapes to characters much as C does. ...
    (comp.lang.forth)
  • Re: A note on computing thugs and coding bums
    ... code is valid for any character set that is legal in C (which is a ... characters in the required source character set ... A String, in C Sharp or Java, can be redefined. ... allow programmers to handle some other data format, ...
    (comp.programming)