Android Question split with regex

tufanv

Expert
Licensed User
Longtime User
Hello,

I am receiving an entry like this from remote database:
B4X:
BTC---Bitcoin---0.5,ETH---Ethereum---15,.......

My question is how can i use regex to get 3 values like :
BTC Bitcoin 0.5
ETH Ethereum 15
seperately so insert them to database as I like. ?

Regex always blows my mind..
 

sorex

Expert
Licensed User
Longtime User
use this pattern

BTC---Bitcoin---(.*),ETH---Ethereum---(.*),

untested code tested code

B4X:
Dim m As Matcher
m=Regex.matcher("BTC---Bitcoin---(.*),ETH---Ethereum---(.*),","BTC---Bitcoin---0.5,ETH---Ethereum---15,")
m.find
Log("bitcoin:"& m.Group(1))
Log("eth:"& m.Group(2))'                     -       -       -       -       -
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
From the type documentation, exactly what you want:
Split (Pattern As String, Text As String) As String()
Splits the given text around matches of the pattern.
Note that trailing empty matches will be removed.
Example:
Dim components() As String
components = Regex.Split(",", "abc,def,,ghi") 'returns: "abc", "def", "", "ghi"

You can replace the "-" with blanks using string replace method.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
sorry, I misread the first post. you want all 3 of them not only the value.

then use this...

B4X:
Dim m As Matcher
m=Regex.matcher("(.*)---(.*)---(.*)","BTC---Bitcoin---0.5,ETH---Ethereum---15,".Replace(",",CRLF) )
Do While m.find
 Log(m.Group(1) &" "& m.Group(2) &" "& m.Group(3))
Loop

Waiting for debugger to connect...
Program started.
BTC Bitcoin 0.5
ETH Ethereum 15
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
sorry, I misread the first post. you want all 3 of them not only the value.

then use this...

B4X:
Dim m As Matcher
m=Regex.matcher("(.*)---(.*)---(.*)","BTC---Bitcoin---0.5,ETH---Ethereum---15,".Replace(",",CRLF) )
Do While m.find
 Log(m.Group(1) &" "& m.Group(2) &" "& m.Group(3))
Loop

Waiting for debugger to connect...
Program started.
BTC Bitcoin 0.5
ETH Ethereum 15
Thanks !
 
Upvote 0
Top