6 Replies - 302 Views - Last Post: 16 October 2013 - 08:40 AM Rate Topic: -----

#1 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 184
  • Joined: 10-March 12

ordered occurances of substring in string

Posted 16 October 2013 - 06:13 AM

Hello. I have a what it seemed at the beginning an easy problem, and which is killing my brains now X(
So. Lets say i have the following string:

 "[[aaaa:bbbb ccccc dddddd eee.ffff|gggg hh [[iii jj]] kk [[lll]] mmm nn]][[ooo pppp [[rrr]]]]" 


what i need to do is replace certain substrings with some other substrings. For example: replace "[[iii jj]]" with "<a href=iii jj>", "[[lll]]" with "<a href=llll>" and so on. That would not be a problem, if i didnt suppose to open and close them IN THE SAME ORDER they are in the original string, meaning that if i have groups inside other groups like so: [[ [[]] [[]] ]] i need to replace the ones inside first and the outer ones last. Its is basically something like at the math with the paranthesis: every opened one has a closing one, in a certain order of closing. How would i go about this? All i got until now is replacing the begining of a bracket and the ending of a second (jumping over the second begining!). Here is what i used so far:



  Private Shared Function _parse_links(ByVal _content As String)
        Dim _search_pattern As String = "[["
        Dim position As Integer = 0
        Dim _trim_link As String
        Dim _displayed_link As String

        While position <> -1
            position = _content.IndexOf(_search_pattern, position)
          
            If position <> -1 Then

                Dim endPosition As Integer = _content.IndexOf("]]", position + 1) + 2


                If endPosition <> -1 Then
                    If Not _content.Substring(_content.IndexOf(_search_pattern, position), 6).Contains("File") Then
                        Dim _link As String
                        _link = _content.Substring(position, endPosition - position)
                        _link = _link.Remove(0, 2)
                        _link = _link.Remove(_link.IndexOf("]]"))
                        _displayed_link = _link
                        If _displayed_link.Contains("|") Then
                            _displayed_link = _displayed_link.Remove(0, _displayed_link.LastIndexOf("|") + 1)
                        End If
                        If _link.Contains(" ") Then
                            _trim_link = _link.Replace(" ", "_")
                        Else
                            _trim_link = _link
                        End If
                        If _trim_link.Contains("|") Then
                            Dim _alt As String

                            _alt = _trim_link.Substring(0, _trim_link.LastIndexOf("|"))
                            _trim_link = _trim_link.Remove(_trim_link.LastIndexOf("|"))
                            _content = _content.Replace(_content.Substring(position, endPosition - position), "<a STYLE=""TEXT-DECORATION: NONE"" href=""" & _trim_link & """ title=""" & _alt & """>" & _displayed_link & "</a>")
                        Else
                            _content = _content.Replace(_content.Substring(position, endPosition - position), "<a STYLE=""TEXT-DECORATION: NONE"" href=""" & _trim_link & """>" & _displayed_link & "</a>")
                        End If
                        isbns.Add(_content.Substring(position, endPosition - position))
                    End If
                End If
                position = endPosition
            End If
        End While
        Return _content
    End Function


'parameter _content passes the following value: "[[File:Sisko outlines operation return.jpg|thumb|The battle plan of [[Operation Return]] from [[2374]] as depicted on an LCARS terminal]]
[[File:Long range scan 081588.jpg|thumb|LCARS as seen on the ''Enterprise''-E in [[2379]]]]"



Is This A Good Question/Topic? 0
  • +

Replies To: ordered occurances of substring in string

#2 ybadragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 151
  • View blog
  • Posts: 968
  • Joined: 11-May 12

Re: ordered occurances of substring in string

Posted 16 October 2013 - 06:18 AM

Can you tell us what the output should look like for the example given, so we can better understand how the formatting should work?
Was This Post Helpful? 0
  • +
  • -

#3 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 184
  • Joined: 10-March 12

Re: ordered occurances of substring in string

Posted 16 October 2013 - 06:35 AM

the output should look like this:
[[aaaa means <table><tr><td><img src=exemple.jpg>
[[iii jj]] means <a href="example.html">
[[lll]] means <a href="example2.html">
mmm nn]] means </td></tr></table>

so, basically, some [[]] substrings are table cells with images, other [[]] are links, inside those cells. this line [[aaaa:bbbb ccccc dddddd eee.ffff|gggg hh [[iii jj]] kk [[lll]] mmm nn]] would look like
<table><tr><td><img src=exemple.jpg><a href="example.html"><a href="example2.html"></td></tr></table>
Was This Post Helpful? 0
  • +
  • -

#4 ybadragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 151
  • View blog
  • Posts: 968
  • Joined: 11-May 12

Re: ordered occurances of substring in string

Posted 16 October 2013 - 07:22 AM

Ok, so the way I see it, regex would probably be the way to go. I don't know much about regex, so I'm sure there are better ways of achieving exactly what you need done better than this regex does. So what this regex will do is get the internal matches, then after that you can replace those matches in your string, to be what you want aka <a href=example2.html"> then run the regex expression on the string again, until there are no matches.

Like I said, this is probably not the most efficient way for the regex, but it works.

(\[\[[a-zA-Z\|1-9_ .:<>"/\\]{1,}\]\])


If you want to test it, copy the regular expression into this webpage, and your string to test. you will see it highlight the groups

Regex Hero
Was This Post Helpful? 1
  • +
  • -

#5 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 184
  • Joined: 10-March 12

Re: ordered occurances of substring in string

Posted 16 October 2013 - 07:24 AM

i have never used regex before, though i heard of it. i will test it out, thank you! :)
Was This Post Helpful? 0
  • +
  • -

#6 ybadragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 151
  • View blog
  • Posts: 968
  • Joined: 11-May 12

Re: ordered occurances of substring in string

Posted 16 October 2013 - 07:59 AM

There is an error in the above regex, it doesn't like to match if there is an = in the string, working on it now.
Was This Post Helpful? 0
  • +
  • -

#7 ybadragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 151
  • View blog
  • Posts: 968
  • Joined: 11-May 12

Re: ordered occurances of substring in string

Posted 16 October 2013 - 08:40 AM

Ok, I figured out how to include "=" in the string

(\[\[[(*=*)a-zA-Z\|1-9_ .:<>"/\\]{1,}\]\])



the edition of (*=*) allows for the = in the href

this is the breakdown for the regex

(                             ' begin pattern
\[\[                          ' replace \[\[ with the delimiter of your choice. The reason for the \ is because [ is a special character in regex
[(*=*)a-zA-Z\|1-9_ .:<>"/\\]  ' the arguments that are allowed in the string
{1,}                          ' repeat the regex as many times as it can
\]\]                          ' replace \]\] with the delimiter of your choice. The reason for the \ is because ] is a special character in regex
)                             ' end pattern

This post has been edited by ybadragon: 16 October 2013 - 08:43 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1