Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Regex replace text in string (ASP)

Posted on 2007-03-23
4
617 Views
Last Modified: 2012-05-05
I have a string that contains HTML page content, scattered throughout will be HREF's in the format of:
  <A HREF="text here ending in a /">test</A>
  <A HREF="text here ending in something else">test</A>

I would like to construct the most efficient regex replace function possible that would insert the string:
  onclick="myevent;"
in the 'A HREF' tags where the the location text ends in a "/", such as:
  <A HREF="text here ending in a /" onclick="myevent;">

The remaining 'A HREF' tags where the location text did not end in "/" would not be modified.
0
Comment
Question by:ddrudik
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:exoska
ID: 18778409
 <A HREF="text here ending in a /">test</A> asfsaf asf s sa
  asdfsadfs<A HREF="text here ending in something else">test</A> sadf asdf asf saf <A HREF="text here ending in a /">test</A>
  <A HREF="text here ending in something else">test</A>

lets say this is the mixed up text

and this is the try;

Dim ResultString, myRegExp
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Multiline = True
myRegExp.Global = True
myRegExp.Pattern = "<(a*)([^/]*)/"">(.*?)</\1>"
ResultString = myRegExp.Replace(SubjectString, "<$1 $2 /"" onclick=""myevent;"">$3</$1>")

please take care of the result and the subject strings..

0
 
LVL 27

Author Comment

by:ddrudik
ID: 18778938
That didn't seem to work for me, as no text is added to the references when tested.

This A HREF where the location reference does not end in '/':
<A HREF="?c=a&d=/testfile.foo">
Should be replaced with:
<A HREF="?c=a&d=/testfile.foo" onclick="myevent;">

This A HREF where the location reference ends in '/':
<A HREF="?c=a&d=/">
Should not be modified.

Thanks for your help.
0
 
LVL 3

Accepted Solution

by:
exoska earned 500 total points
ID: 18779307
oh vice-versa( or a little confusing :))

well if you want to replace that type ( <A HREF="?c=a&d=/testfile.foo"> ) below works

Dim ResultString, myRegExp
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Multiline = True
myRegExp.Global = True
myRegExp.Pattern = "<(a*)([^/]*)/([A-Za-z0-9]?[^>]+)"">"
ResultString = myRegExp.Replace(SubjectString, "<$1 $2$3/"" onclick=""myevent;"">")

it does not replace <A HREF="?c=a&d=/"> type

im not sure so if it s like  a <A HREF="?c=a&d=/testfile.foo">test</a> type the one
below works( but closing tag(</A>) is required for this one)
'the one with the closing tag
Dim ResultString, myRegExp
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Multiline = True
myRegExp.Global = True
myRegExp.Pattern = "<(a*)([^/]*)/([A-Za-z0-9]?[^>]+)"">([^<]+)(</\1>)"
ResultString = myRegExp.Replace(SubjectString, "<$1 $2$3/"" onclick=""myevent;"">$4$5")

0
 
LVL 27

Author Comment

by:ddrudik
ID: 18779743
The first one makes no string replacement and the second one make changes to all A HREF references regardless of if they end in a "/" or not  (remember I need the string only added to those A HREF's that do not end in a "/".  I also had to remove that "/" in the replace command since I don't want a trailing "/" to be added to all references.

I think I can work with your second one by just adding another replacement later for all:
/" onclick="myevent;" with nothing in order to remove the extra ones added by the regex.

If you come up with a modification so I don't have to add another replace that would be better.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question