B4J Question How to get all enclosed strings with #?

Discussion in 'B4J Questions' started by Mashiane, Aug 2, 2019.

  1. Mashiane

    Mashiane Expert Licensed User

    Hi there

    Let's assume, this is the master content..

    Code:
    Today #I# got to #experiment# #with# reef. I #needed# this to try #and# see if #I# can #wrap# it with #BANano#.png
    Now I need to return a list that will have

    Code:
    I,experiment,with,needed,and,wrap,BANano
    And kindly need a code snippet for such. Preferably not RegEx please.

    Thanks
     
  2. emexes

    emexes Well-Known Member Licensed User

    I know you thought you'd never hear these words from me, but: this is probably better done with regex :-/

    Nonetheless... try this:
    Code:
    Dim L As List
    L.Initialize

    Dim S As String = "Today #I# got to #experiment# #with# reef. I #needed# this to try #and# see if #I# can #wrap# it with #BANano#.png"

    Dim OpenHashAt As Int = -1

    For I = 0 To S.Length - 1
        
    If S.CharAt(I) = "#" Then
            
    If OpenHashAt = -1 Then    'if not currently inside a hash field
                OpenHashAt = I
            
    Else    'currently inside a hash field
                L.Add(S.SubString2(OpenHashAt + 1, I))
                OpenHashAt = -
    1    'now outside of hash field, get ready for next one
            End If
        
    End If
    Next I
     
    Last edited: Aug 2, 2019
    MarkusR, Mashiane and LucaMs like this.
  3. vpires

    vpires Member Licensed User

    Well ... with the regEx stuff that you don't want. Lol.
    Code:
    Dim text As String ="Today #I# got to #experiment# #with# reef. I #needed# this to try #and# see if #I# can #wrap# it with #BANano#.png"
        
    Dim pattern As String = "#(.*?)#"
        
    Dim Matcher1 As Matcher
        
    Dim l As List : l.initialize
       
        Matcher1 = 
    Regex.Matcher(pattern, text)
        
    Do While Matcher1.Find
            
    Log("Found: " & Matcher1.Group(1))
            l.Add(Matcher1.Group(
    1))
        
    Loop
     
    MarkusR, Erel, Mashiane and 1 other person like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice