Android Question Regex.Ismatch2 not matching Regex.Matcher2

epiCode

Active Member
Licensed User
Is this a weird issue or am I missing something here?
Ismatch2 is not matching but with .Matcher2 and same pattern and string it matches.


B4X:
Dim str="This is a sample line but it is Highly recommen4ded to not rely on speculations"
Dim pattern As String = "([^a-zA-Z]|^)("& "high" &")"

If Regex.IsMatch2( pattern, Regex.CASE_INSENSITIVE ,str) Then Log("isMatch matched")

Dim m As Matcher = Regex.Matcher2( pattern, Regex.CASE_INSENSITIVE,str)
If m.Find Then Log("Matcher2 Matched")
 

emexes

Expert
Licensed User
IsMatch2 compares pattern to the entire string.
Matcher finds occurrences of pattern within the string.

Similar to .CompareTo vs .Contains (or "=" vs INSTR in older BASIC dialects)

Well, except for the case-insensitive bit, so really it'd be more like eg MyBigString.ToLowerCase.Contains(MyLittleString.ToLowerCase)
 
Upvote 0

emexes

Expert
Licensed User
B4X:
Dim str="This is a sample line but it is Highly recommen4ded to not rely on speculations"
Dim pattern As String = "([^a-zA-Z]|^)("& "high" &")"

What is the end goal?

Eg, my first guess was that you were searching for the high temperature within a weather forecast,

and the [^a-zA-Z] was a roundabout way of matching a ° degree symbol,
 
Upvote 0

epiCode

Active Member
Licensed User
IsMatch2 compares pattern to the entire string.
Matcher finds occurrences of pattern within the string.

Similar to .CompareTo vs .Contains (or "=" vs INSTR in older BASIC dialects)

Well, except for the case-insensitive bit, so really it'd be more like eg MyBigString.ToLowerCase.Contains(MyLittleString.ToLowerCase)
That solves it for me. Guess m.Find is the way to go :)

What is the end goal?

Eg, my first guess was that you were searching for the high temperature within a weather forecast,

hahaha

The end goal was to match all occurrences which are not (in the middle of a "word") OR beginning of the line
 
Upvote 0

emexes

Expert
Licensed User
The end goal was to match all occurrences which are not (in the middle of a "word") OR beginning of the line

Made slightly harder than usual by the "not ... beginning of the line" criterion, but maybe this is in the ballpark:

B4X:
Dim SampleText As String = $"As Manturov delivered his report during a video conference
call with leading government ministers on Wednesday, Putin
fidgeted and made notes before interrupting him.

“When will there be contracts? Directors have told me that
they have no contracts,” the Russian president said,
referring to efforts to acquire new aircraft. “You are not
trying hard enough,” a visibly angered Putin said. He later
raised his voice and said: “Why are you playing a fool?”

When Manturov said he would try to make sure the job would
be done during the first quarter of the year, Putin snapped
back that it should be done sooner. “You do it within a
month, no later than that,” he said.

Manturov tried to defend himself but this appeared to
irritate Putin further during his government’s first
meeting of 2023 which was broadcast on state TV.
"$
B4X:
Dim pat As String = "[^\na-zA-Z]([a-zA-Z]+)"    'words (clumps of letters) not at start of line
 
Dim mat As Matcher = Regex.Matcher(pat, SampleText)
 
Do While mat.Find
    Log(mat.Group(1))
Loop

Log output:
Waiting for debugger to connect...
Program started.
Manturov
delivered
his
report
during
a
video
conference
with
leading
government
ministers
on
Wednesday
Putin
and
made
notes
before
interrupting
him
When
will
there
be
contracts
Directors
have
told
me
that
have
no
contracts
the
Russian
president
said
to
efforts
to
acquire
new
aircraft
You
are
not
hard
enough
a
visibly
angered
Putin
said
He
later
his
voice
and
said
Why
are
you
playing
a
fool
Manturov
said
he
would
try
to
make
sure
the
job
would
done
during
the
first
quarter
of
the
year
Putin
snapped
that
it
should
be
done
sooner
You
do
it
within
a
no
later
than
that
he
said
tried
to
defend
himself
but
this
appeared
to
Putin
further
during
his
government
s
first
of
which
was
broadcast
on
state
TV
Program terminated (StartMessageLoop was not called).
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
I'm still wondering what the higher-level goal is.

It sounds like you're looking to scrape a webpage or document, looking for high scores or high tides or OHLC prices or parcel dimensions or mountain hut altitudes or... am I completely off-track? ?
 
Upvote 0
Top