[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Parsing a text string (Regular expressions)

Posted on 2006-04-12
16
Medium Priority
?
278 Views
Last Modified: 2012-08-13
Hi all,

I am looking for a simple solution to my problem.

I have a text string which contains several pieces of information enclosed in '[]' tags. I am looking for an example piece of code which will parse through the text and replace these tags with other information.

For example:

String = "Hello [foo] world. How are you [bar] today?"

output = "Hello Chris world. How are you Hughes today?"

Thanks..
0
Comment
Question by:chrishughes
  • 6
  • 4
  • 4
  • +1
16 Comments
 

Author Comment

by:chrishughes
ID: 16436748
But I should clarify - I can't use a direct replace statement because the text between the []'s determines the replacement text.
0
 
LVL 6

Expert Comment

by:John_Lennon
ID: 16437696
how can you know what word put instead of another?

you can try something like

'####################
txtOriginal = "Hello [foo] world. How are you [bar] today?"
do while instr(txtOriginal, "[") > 0
  intFirts = instr(txtOriginal, "[")
  intFinal = instr(txtOriginal, "]")
  oldTxt = mid(txtOriginal, intFirst + 1, intFinal - 1)
  newTxt = replaceText(oldTxt)
  output = replace(txtOriginal, "[" & oldTxt & "]", newTxt)
loop
'output = "Hello Chris world. How are you Hughes today?"

function replaceText(Word)
  dim strTmp
  select case word
    case "foo"
      strTmp = "Chris"
    case "bar"
      strTmp = "Hughes"
  end select
end function
'####################

but you have to put all the words in the case in replaceText function,

how do you know the replacement text? you got that in a DB? or you use a case sentence like mine?
0
 
LVL 6

Expert Comment

by:John_Lennon
ID: 16437716
sorry, you have to include this lines of code in the case statement to prevent infinite looping

case else
  strTmp = ""
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 22

Expert Comment

by:WMIF
ID: 16438467
let me first say that you dont need regular expressions for this.  if you have a list of values to replace, then just run all those values on the string.  if the replace function finds the value you tell it to look for, it will replace the value.  if it doesnt find the value, it simply bails without changing anything.

thestring = "Hello [foo] world. How are you [bar] today?"

thestring = replace(thestring, "[foo]", "Chris")
' gives = "Hello Chris world. How are you [bar] today?"

thestring = replace(thestring, "[not_there]", "Fred")
' gives = "Hello Chris world. How are you [bar] today?"

thestring = replace(thestring, "[bar]", "Hughes")
' gives = "Hello Chris world. How are you Hughes today?"




if you have a db list of values to replace, then just get those values and loop.

query = "select keyword, replacewith from terms"
do until rs.eof
  thestring = replace(thestring, rs("keyword"), rs("replacewith"))
  rs.movenext
loop



let me know if this cant meet your needs.
0
 
LVL 8

Expert Comment

by:Leo Eikelman
ID: 16438615
Wouldn't it be easier to just create a method where you pass these words as a parameter?

Public Sub CreateText(FirstName,SecondName)
  Response.Write "Hello " & FirstName & " world. How are you " & SecondName & " today?"
End Sub

Cheers,

Leo
0
 

Author Comment

by:chrishughes
ID: 16439543
Hi guys,

Ok I may have over simplified what I was after  and thus not been clear. I will give you the full problem and I beleive that I do need to us regular expressions:

I have a text string such as : "Hello world [how] are [you] today?" which is going to form text on a webpage and can contain any number of words or phrases wrapped in [ ]'s.

I want to parse the string and turn these words into links. For example this example would become: "Hello world <a href="home.asp?p=how">how</a> are <a href="home.asp?p=you">you</a> today?"

So I cannot use a replace statement as the words in the [ ]'s could be anything.

Currently the closest I have got is with this code:

dim regex
Set regEx = New RegExp
regEx.Global = true
regEx.IgnoreCase = True
      
regEx.Pattern = "([\[](\w| )+[\]])"
txt= regEx.Replace(txt, "<a href='?p=$1'>$1</a>")

However this still leaves the [ ]'s in in the new string. I therefore need to find some way to manipulate $1 to remove the [ and ] before creating the new string.

Any ideas?

Chris

0
 

Author Comment

by:chrishughes
ID: 16439573
In answer to leikelman: This is simply not possible as the text is being pulled out of a database and it is generic not a set pattern!

Thanks anyway!
0
 
LVL 8

Expert Comment

by:Leo Eikelman
ID: 16439582
once again, why do u not use a method to build the string?

As I mentioned in my first comment.  You can just pass the words you want into the function and create the pages.  Why use regular expressions?

Cheers,

Leo
0
 
LVL 8

Expert Comment

by:Leo Eikelman
ID: 16439605
I posted without seeing the updated comment you made.  could you explain the 'generic' text that is returned from the database?  Is there always two links?

Leo
0
 

Author Comment

by:chrishughes
ID: 16439639
The problem is the user putting the text into the database can use any tags. So regular expressions are needed to keep this simple. And there can be any number of tags.

Regular expressions seems to be the perfect solution for me, I just cannot figure out how to lose the [ and ].

Thanks for your help!

Chris
0
 

Author Comment

by:chrishughes
ID: 16439648
By tags I mean the name between the [ ]'s.
0
 
LVL 8

Expert Comment

by:Leo Eikelman
ID: 16439759
Ok so you want to get rid of the [] but want to keep what is inside the brackets to use for the link?

You can do it like this

<%
InitialString = "hello [how] are u"

Set RegularExpressionObject = New RegExp

With RegularExpressionObject
.Pattern = "[[]"
.IgnoreCase = True
.Global = True
End With

'"<a href='home.asp?p=how'>" is what is returned from the database
ReplacedString = RegularExpressionObject.Replace(InitialString, "<a href='home.asp?p=how'>")


With RegularExpressionObject
.Pattern = "[]]"
.IgnoreCase = True
.Global = True
End With

ReplacedString = RegularExpressionObject.Replace(ReplacedString, "</a>")

Response.Write "Replaced " & InitialString & "<br> with " & ReplacedString

Set RegularExpressionObject = nothing
%>

run this code in an ASP page and see if this is your desired results


Leo


0
 
LVL 22

Expert Comment

by:WMIF
ID: 16440521
you had the pattern close.  check this out:

\[([\w ]+)\]

still replacing with $1
0
 
LVL 22

Accepted Solution

by:
WMIF earned 500 total points
ID: 16440565
old - ([\[](\w| )+[\]])
new - \[([\w ]+)\]

first off, you wanted to have word characters or spaces im assuming from your pattern.  put the \w and " " inside the square braces.  next thing there, you had the grouping applied to the single character there.  add the square braces inside your round braces and you create your group.  only problem then was that you had the "1 or more" + sign outside the grouping.  your $1 was returning the entire thing.  $2 was closer to what you wanted, only $2 was returning a single character with the + outside the grouping.  once thats straightened out, you can remove the addition square braces surrounding the escaped square braces because they are not needed and just add confusion.  do you follow what was going on?
0
 

Author Comment

by:chrishughes
ID: 16468646
WMIF - that was a perfect answer not only did it solve my problems, but I now feel like I understand regular expressions a little more!

Thank you
0
 
LVL 22

Expert Comment

by:WMIF
ID: 16470830
they are tough, but you will catch on.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline
Suggested Courses

825 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