Solved

Regular Expression

Posted on 2004-04-14
7
329 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now