Solved

Regular Expression

Posted on 2004-04-14
7
333 Views
Last Modified: 2010-04-17
I'm trying to develop a regular expression to match a space character in a block of code, but it can't
be contained within a hyperlink.

-------------------------
<a href="http://somwhere.com">This is the link, but I don't want any match in here.</a>  But any space
after this would be OK.  There also may not be any hyperlink in the text, in which case the first space found would be great.
-------------------------

In the above code, I would want the position of the first space after the closing </a>  If there was no hyperlink, then I would want the position of the first space in the paragraph.  I've been working on regular expression stuff for a couple of weeks but don't have any clue how to go about this.

Any tips?
0
Comment
Question by:vision69
  • 3
7 Comments
 
LVL 6

Expert Comment

by:BAlexandrov
ID: 10821062
It would be great if you explain wxactly what are you trying to do. Why you need position of first space - probbaly you want to insert something there or get text before and after it.
0
 
LVL 6

Expert Comment

by:BAlexandrov
ID: 10821073
Probbaly this is waht you need

\(?:<a.*<\/a>)( )\im
0
 

Author Comment

by:vision69
ID: 10821625
I have a piece of software that breaks up a block of text into pieces.  I want to insert (at random) a URL in the block of text.  But I don't want to accidently insert a hyperlink, inside another hyperlink...  That is why I'm looking for the first occurance of a space that is not inside an already existing hyperlink.
0
 
LVL 6

Accepted Solution

by:
BAlexandrov earned 250 total points
ID: 10821937
I do not know how you will find a random place -this is your part.

If it was ASP with Javascript then this will do it - get first and third match and insert something between them

<%@ Language=JScript %>
<%
var reg  = /(<a.*<\/a>)( )(.*)/im
var text = "<a href=\"http://somwhere.com\">This is the link, but I don't want any match in here.</a>  But any space after this would be OK."
Response.Write (text.replace(reg,"$1new text$3"));
%>


0
 
LVL 10

Assisted Solution

by:Kavar
Kavar earned 250 total points
ID: 10823195
/(<a[^xfe]*a\>)*?(/s)/im
$2 will be the white space

if you want to accomodate both then
/((<a[^xfe]*a\>)*?(/s)|(/s))/im



0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Split string on commas but not when enclosed in parentheses 7 55
regex expression 9 60
This code tracks birthdays 3 73
Definitions and default visual studio colors 5 65
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This is about my first experience with programming Arduino.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

773 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