B4J Question [BANano] How do I create a SplitWords sub using RegEx?

Mashiane

Expert
Licensed User
Longtime User
Update: This is my current attempt which is not working.

B4X:
Public Sub SplitWords(s As String) As String()
    Dim x As BANanoRegEx
    x.InitializePattern("\W+")
    Dim res() As String = x.ExecAll(s)
    Return res
End Sub

Output:

B4X:
// [232] Public Sub SplitWords(s As String) As String()
this.splitwords=function(_s) {
if (_B==null) _B=this;
var _x,_res;
// [233]  Dim x As BANanoRegEx
_x=null;
// [234]  x.InitializePattern( {201} )
_x=new RegExp(\W+);
// [235]  Dim res() As String = x.ExecAll(s)
_res=(_foundRES=_x.exec(_s), _foundRES == null) ? null: _foundRES;
// [236]  Return res
return _res;
// End Sub
};
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
I'm not very familiar with regex myself, but maybe something like this:
B4X:
    Dim str As String = "This is a test I'm doing to split a sentence into words."
    Dim RegExp As BANanoRegEx
    RegExp.InitializePattern("/\b[^\s]+\b/g")
    Dim found As String = RegExp.Exec(str)
    Do While found <> Null
        Log(found)
        found = RegExp.Exec(str)
    Loop

Alwaysbusy
 
Upvote 0
Top