Solved

Regular Expression

Posted on 2004-04-14
7
335 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
[X]
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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.

738 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