Android Question Regular Expression in b4A to highlight a pattern word anywhere in a string

beelze69

Active Member
Licensed User
Longtime User
Hi,

I would like to highlight a particular word-pattern either individually, within or end of string in b4A

For example:

The pattern 'rail' should be highlighted in red colour in the sentence 'Railway tracks are made of iron. The rail ran over the dog and its entrails came out'.

The sequence of letters marked in 'underline' should be highlighted in the above example.

i) What is the regex expression to match this pattern and how should the word be highlighted ?

Thanks..
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

Pattern: "rail"
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User

Pattern: "rail"

What the, 2017 @Erel, where on earth did that post come from, and how did I somehow missed it :mad:

That's an excellent solution that I never even knew already existing. Thank you for sharing, again ?
 
Last edited:
Upvote 0

beelze69

Active Member
Licensed User
Longtime User
Hi Erel !

Sorry... But I am unable to make it work... Attaching the source code ... I am getting the following error. Also pasted your calling code example string.
Please help.

Thanks..

:BEGIN ERROR:
Logger connected to: ......
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_markpattern (java line: 422)
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
at java.util.regex.Matcher.start(Matcher.java:1159)
at anywheresoftware.b4a.keywords.Regex$MatcherWrapper.GetStart(Regex.java:174)
at b4a.example.main._markpattern(main.java:422)
at b4a.example.main._activity_create(main.java:367)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at b4a.example.main.afterFirstLayout(main.java:105)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_markpattern (java line: 422)
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
at java.util.regex.Matcher.start(Matcher.java:1159)
at anywheresoftware.b4a.keywords.Regex$MatcherWrapper.GetStart(Regex.java:174)
at b4a.example.main._markpattern(main.java:422)
at b4a.example.main._activity_create(main.java:367)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at b4a.example.main.afterFirstLayout(main.java:105)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

:END ERROR:
 

Attachments

  • Regex Example.zip
    10.4 KB · Views: 261
Upvote 0

beelze69

Active Member
Licensed User
Longtime User
The group number in the first pattern is wrong.
It should be:
B4X:
Dim cs1 As CSBuilder = MarkPattern(s1, "Rail", 0)
Dim cs2 As CSBuilder=MarkPattern(s2,"\B(#\w+)\b",1)

Thanks Erel !

It worked ! .. I also changed the pattern to (?i)(rail) (so it can be case insensitive match)..Hope it is correct ..

However, did not understand the purpose of Group Number..

Thanks..
 
Upvote 0
Top