tufanv Expert Licensed User Longtime User Jan 17, 2018 #1 Hello, I am receiving an entry like this from remote database: Code: Copy 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..
Hello, I am receiving an entry like this from remote database: Code: Copy 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..
S sorex Expert Licensed User Longtime User Jan 17, 2018 #2 use this pattern BTC---Bitcoin---(.*),ETH---Ethereum---(.*), untested code tested code Code: Copy 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: Jan 17, 2018 Upvote 0
use this pattern BTC---Bitcoin---(.*),ETH---Ethereum---(.*), untested code tested code Code: Copy 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))' - - - - -
derez Expert Licensed User Longtime User Jan 17, 2018 #3 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
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.
S sorex Expert Licensed User Longtime User Jan 17, 2018 #4 sorry, I misread the first post. you want all 3 of them not only the value. then use this... Code: Copy 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
sorry, I misread the first post. you want all 3 of them not only the value. then use this... Code: Copy 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
tufanv Expert Licensed User Longtime User Jan 17, 2018 #5 sorex said: sorry, I misread the first post. you want all 3 of them not only the value. then use this... Code: Copy 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 Click to expand... Thanks ! Upvote 0
sorex said: sorry, I misread the first post. you want all 3 of them not only the value. then use this... Code: Copy 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 Click to expand... Thanks !