B4J Question Regular Expression Multiline option not functional

xulihang

Active Member
Licensed User
Longtime User
Hi,

I use this code to match the content in p11 tag, but did not get the right result.

B4X:
    Dim text As String
    text=$"
    <p11><c14>Technology</c14><c0>—Devices or techniques that solve problems or help people carry out everyday tasks.</c0>
<c14>Texture</c14><c0>—The look or feel of an object’s surface layer.</c0>
<c14>Thrust</c14><c0>—A force that pushes objects forward or upward.</c0></p11>
"$
    Dim matcher As Matcher
    matcher=Regex.Matcher2("<p\d+>.*?</p\d+>",Regex.MULTILINE,text)
    Do While matcher.Find
        Log(matcher.Match)
        ListView1.Items.Add(matcher.Match)
    Loop
 

xulihang

Active Member
Licensed User
Longtime User
Upvote 0

PatrikCavina

Active Member
Licensed User
Try this code:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    Dim txt As String = $"<p11><c14>Technology</c14><c0>—Devices or techniques that solve problems or help people carry out everyday tasks.</c0>
    <c14>Texture</c14><c0>—The look Or feel of an object’s surface layer</c0>
    <c14>Thrust</c14><c0>—A force that pushes objects forward Or upward.</c0></p11>"$
    
    Dim m As Matcher = Regex.Matcher2("(?><p\d+>)(.*)(?=<\/p\d+>)", 32, txt)
    m.Find
    Log(m.Group(1)) '<----Group 1 contains information you needing
End Sub

When i worked with regex a big help for me was this site: https://regex101.com/
 
Upvote 0

PatrikCavina

Active Member
Licensed User
Sorry, use
B4X:
Dim m As Matcher = Regex.Matcher2("(?><p\d+>)(.*?)(?><\/p\d+>)", 32, txt)
This makes regex ungreedy so you can extract each content between <pXX> tags, where XX is a number with undefined length.
B4X:
    Dim txt As String = $"
    <p11><c14>Technology</c14><c0>—Devices or techniques that solve problems or help people carry out everyday tasks.</c0>
    <c14>Texture</c14><c0>—The look Or feel of an object’s surface layer</c0>
    <c14>Thrust</c14><c0>—A force that pushes objects forward Or upward.</c0></p11>
 
    <p11>Example text</p11>
 
    "$
 
    Dim m As Matcher = Regex.Matcher2("(?><p\d+>)(.*?)(?><\/p\d+>)", 32, txt)
    Do While m.Find
        Log("Found: " & m.Group(1))
        Log("-------------------")
    Loop

Result will be

B4X:
Found: <c14>Technology</c14><c0>—Devices or techniques that solve problems or help people carry out everyday tasks.</c0>
    <c14>Texture</c14><c0>—The look Or feel of an object’s surface layer</c0>
    <c14>Thrust</c14><c0>—A force that pushes objects forward Or upward.</c0>
-------------------
Found: Example text
-------------------
 
Last edited:
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
Try this code:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    Dim txt As String = $"<p11><c14>Technology</c14><c0>—Devices or techniques that solve problems or help people carry out everyday tasks.</c0>
    <c14>Texture</c14><c0>—The look Or feel of an object’s surface layer</c0>
    <c14>Thrust</c14><c0>—A force that pushes objects forward Or upward.</c0></p11>"$
   
    Dim m As Matcher = Regex.Matcher2("(?><p\d+>)(.*)(?=<\/p\d+>)", 32, txt)
    m.Find
    Log(m.Group(1)) '<----Group 1 contains information you needing
End Sub

When i worked with regex a big help for me was this site: https://regex101.com/

Thanks! Using a group is a good way to extract text.
 
Upvote 0
Top