using Matcher to extract a number from a stream of data received via a bluetooth scan

alvinmb

Member
Licensed User
Longtime User
Im extracting a number form a string using

pattern = pattern = "\d+"

' Test input string is

Dim Matcher1 As Matcher
Matcher1 = Regex.Matcher(pattern, Msg)
Do While Matcher1.Find
Log("Found Barcode: " & Matcher1.Match)
Loop


and I want to assign the returned string to a label

barcode_label.Text = Matcher1.Match.Trim

And this fails with a run time error
what am i doing wrong?
 
Last edited:

pluton

Active Member
Licensed User
Longtime User
Please can you post again test string because I don't see characters I see some ?????:


I test it now and If i put

B4X:
pattern = "\d+" 'result is 061650000050444154

Then If I put
B4X:
barcode_label.Text = Matcher1.Match
 
Last edited:
Upvote 0

alvinmb

Member
Licensed User
Longtime User
OK more info.

I'm reading a the input from a barcode scanner using

If AStream.IsInitialized = False Then
AStream.Initialize(serial1.InputStream,Null, "AStream")
End If

What I get from the scanner is the following:
""

What I want to do is extract all the numbers (18) in this case, however the length can be between 9 and 24 digits - which is a barcode.

I extract the digits using

Matcher1 = Regex.Matcher(pattern, Matcher1.Match)
and I can log the result using

Log("Found Barcode: " & Matcher1.Match)

BUT when I try and assign Matcher1.Match to a text label I get this dump


java.lang.IllegalStateException: No successful match so far
at java.util.regex.Matcher.ensureMatch(Matcher.java:607)
at java.util.regex.Matcher.group(Matcher.java:358)
at anywheresoftware.b4a.keywords.Regex$MatcherWrapper.Group(Regex.java:138)
at com.idealss.com.bticketmanager.main._logmessage(main.java:1840)
at com.idealss.com.bticketmanager.main._astream_newdata(main.java:729)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA$3.run(BA.java:296)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3688)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
java.lang.IllegalStateException: No successful match so far
 
Upvote 0

alvinmb

Member
Licensed User
Longtime User
Sorry I included the wrong dump
The dump I get is the following
***************
Barcode Read
Found Barcode: 999990000232298841
Found Barcode: 5
main_logmessage (B4A line: 376)
myABBarcode_BarcodeFound (Matcher1.Match.trim, "")
java.lang.IllegalStateException: No successful match so far
at java.util.regex.Matcher.ensureMatch(Matcher.java:607)
at java.util.regex.Matcher.group(Matcher.java:358)
at java.util.regex.Matcher.group(Matcher.java:376)
at anywheresoftware.b4a.keywords.Regex$MatcherWrapper.getMatch(Regex.java:151)
at com.idealss.com.bticketmanager.main._logmessage(main.java:1841)
at com.idealss.com.bticketmanager.main._astream_newdata(main.java:729)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA$3.run(BA.java:296)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3688)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
java.lang.IllegalStateException: No successful match so far
** Activity (main) Pause, UserClosed = false **
*********************************
 
Upvote 0

alvinmb

Member
Licensed User
Longtime User
Just to complete this with a solution:

Dim New, barcode, buffer As String
Dim Matcher1 As Matcher

Matcher1 = Regex.Matcher("\d", Msg)
Do While Matcher1.Find
Log("Found Barcode: " & Matcher1.Match)
buffer = Matcher1.Match
barcode = barcode & buffer
Log("Barcode built" & barcode)
Loop

This works
 
Upvote 0
Top