Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

A Digit right after $1 in Replace, I'm fully mess up !

Last post 05-14-2008, 12:19 PM by mash. 5 replies.
Sort Posts: Previous Next
  •  05-13-2008, 1:40 PM 42195

    A Digit right after $1 in Replace, I'm fully mess up !


    Hi there, I've learn RegEx only 1 day and mastered almost the features.(or I think so)

    I'm writing an ASP Page with VBScript, an need to replace a String with the Pattern like this :

    REG.Pattern = "(1A).(2).(3).(4).(5).(6).(7).(8).(9).(10).(11).(12).(13B).(14)"

    OutPut = REG.Replace("1A-2-3-4-5-6-7-8-9-10-11-12-13B-14" , "<b> $1333 </b>")

    What I want to get is : 1A333. But it returns (unexpectedly)  :  13B33 because it think I Needed : $13 but not $1

    I wonder how can I clearly separate the 2 parts with, such an "invisible" character ?
    (Some thing like :  $1<z>333 , but I the tag <z> still exists in the string , it's only invisible when print the webpage)

    Any suggestions are highly appreciated !

    Filed under:
  •  05-13-2008, 3:57 PM 42206 in reply to 42195

    Re: A Digit right after $1 in Replace, I'm fully mess up !

    Why do you need to capture all the numbers if you are only going to replace the first group?

    Can you explain the problem you are trying to solve in more detail?

  •  05-13-2008, 5:04 PM 42211 in reply to 42195

    Re: A Digit right after $1 in Replace, I'm fully mess up !

    gadapchetvoi:

    Hi there, I've learn RegEx only 1 day and mastered almost the features.(or I think so)

    I'm writing an ASP Page with VBScript, an need to replace a String with the Pattern like this :

    REG.Pattern = "(1A).(2).(3).(4).(5).(6).(7).(8).(9).(10).(11).(12).(13B).(14)"

    OutPut = REG.Replace("1A-2-3-4-5-6-7-8-9-10-11-12-13B-14" , "<b> $1333 </b>")

    What I want to get is : 1A333. But it returns (unexpectedly)  :  13B33 because it think I Needed : $13 but not $1

    I wonder how can I clearly separate the 2 parts with, such an "invisible" character ?
    (Some thing like :  $1<z>333 , but I the tag <z> still exists in the string , it's only invisible when print the webpage)

    Any suggestions are highly appreciated !

    Yeah you are kinda hosed regex-wise.   You'll need to do a double replace. Have the regex.replace include some funky text like "$1!@#333" then apply a string replace on your results to remove "!@#"

     If you were using .Net's regex engine this solution would work for you. http://regexadvice.com/blogs/mash/archive/2005/09/28/12925.aspx
    After glancing over that article I apparently came up with another work around for this issue but I don't remember what it was.


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  05-13-2008, 5:16 PM 42213 in reply to 42211

    Re: A Digit right after $1 in Replace, I'm fully mess up !

    It occurred to me that in your case you can use a replace string like "<b> $1</b><b>333 </b>" which, while ugly html, should get you the results you're after.

    In .NET, you can also use <b>${1}333</b> which is the cleanest solution -- I don't know if this will work in VBScript.

  •  05-13-2008, 6:05 PM 42214 in reply to 42213

    Re: A Digit right after $1 in Replace, I'm fully mess up !


    Yeah, I've learned Regexp yesterday morning.
    After some googling and reading almost the regexp references, especially the "Replace" , I've found something wrong with this function immediately. Just  $1...  , so, it missed the close tag.

    And I'm lookin' for an escapse character or some similar things. But, as your replies , there's not exist such a thing.

    Yeah, I'm not trying to do a trick around the limitation , or, to reach any goal.
    I just wanna clarify the RegExp abilities & restrictions. Kind of , if you know how sharp a knife is , you will use it more effectively, since you know what you can cut, and what you can not.

    About using something like : $1!@#333, what if a group of letters like that is existed in the string before? Must I change the group each time I do (depend on the string) ?
    Surely, it's a trick but not a global solution. ( Anyway, it's a quite wide way to go)

    Thanks to all you, I'll finding more and , who know, some next days I'll post the solution here :))
    Agains , thank you indeed.
  •  05-14-2008, 12:19 PM 42256 in reply to 42214

    Re: A Digit right after $1 in Replace, I'm fully mess up !

    gadapchetvoi:

    About using something like : $1!@#333, what if a group of letters like that is existed in the string before? Must I change the group each time I do (depend on the string) ?
    Surely, it's a trick but not a global solution. ( Anyway, it's a quite wide way to go)

    As far as using that approach it would fall on you to know enough about your incoming string to choose a character or sequence unlikely to occur in your text and use that.

    But if this is HTML that is being generated, you can make you replacement string $1&#x0033;33

    The HTML would render with value you'd like

    As a side note I'd also advise using a span tag and CSS or a more appropriate tag than <b>


    OutPut = REG.Replace("1A-2-3-4-5-6-7-8-9-10-11-12-13B-14" , "<span class=""NoticeNumber"">$1&#x0033;33</span>")

    CSS

       .NoticeNumber{ font-weight:bold;}
     


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
View as RSS news feed in XML