You are not logged in.

OlimilO

Intermediate

  • "OlimilO" is male
  • "OlimilO" started this thread

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

1

Tuesday, February 3rd 2009, 12:12am

Like

Hi,

Do you know the Like-Operator in Visual Basic that can be used to compare two strings?

small VB-example:

Jabaco Source

1
2
3
4
5
6
7
8
9
Private Sub Command1_Click()
    Dim s As String
    Dim p As String
    p = "[0-9]*[+-\*/]*[0-9]*=*[0-9]"
    s = "1 + 1 = 2"
    If s Like p Then MsgBox s 'OK
    s = "a + b = c"
    If s Like p Then MsgBox s 'no
End Sub


The syntax is:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
result = string Like pattern
The Like operator syntax has these parts:
Part |  Description
========|=========================================
result |  Required; any numeric variable.
string |  Required; any string expression. 
pattern |  Required; any string expression conforming 
        |            to the pattern-matching conventions 
        |            described in Remarks.
Remarks
If string matches pattern, result is True; 
if there is no match, result is False. 
Characters  |  Matches 
in pattern |  in string
============|=====================================
    ?       |  Any single character.
    *       |  Zero or more characters.
    #       |  Any single digit (0–9).
[charlist]  |  Any single character in charlist.
[!charlist] |  Any single character not in charlist.


the pattern is like a regular expression that can make the Like-operator very likely.

afair Jabaco does not have the Like-operator but of course it does have regular expressions, that can be found in the namespace:

java#util#regex#

maybe Jabaco will sometimes have the Like-Operator meanwhile you can note down a function and use it like the following:

Jabaco Source

1
2
3
4
5
Public Function Like(str As java#lang#String, pat As String) As Boolean
    Dim pt As java#util#regex#Pattern = Pattern.compile(pat, CASE_INSENSITIVE)
    Dim mt As java#util#regex#Matcher = pt.matcher(str)
    Like = mt.find
End Function


even in one line:

Jabaco Source

1
2
3
Public Function Like(str As java#lang#String, pat As String) As Boolean
    Like = java#util#regex#Pattern.compile(pat, CASE_INSENSITIVE).Matcher(str).find
End Function


suggestion: in Jabaco the Like-Operator could be compiled into this one line.

so what, what's the trick here?

OK, the Matcher-object needs a CharSequence-Object, that is the Base-Class of Java#lang#String, StringBuffer and StringBuilder but not of the Jabaco-String-Object.

It is possible to cast a Jabaco-String into a java#lang#String and it is possible to cast a java#lang#string into a Charsequence.

a small example for how to use it in Jabaco:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Public Sub Command1_Click()
   Dim whiteSpacesOrNot As String = "\s*"
   Dim minOneWhitespace As String = "\s+"
   Dim Condition As String = "((\w+)|(\w+\s+[=<>]\s+\w+))"
   Dim p As String 
   p = whiteSpacesOrNot & "If" & minOneWhitespace & Condition & minOneWhitespace & "Then" 
   
   Dim s As String
   
   s = "if true then":         If Like(s, p) Then msgbox s 'ok
   s = "  If False Then  ":    If Like(s, p) Then msgbox s 'ok
   s = "IF b = True THEN":     If Like(s, p) Then msgbox s 'ok
   s = "If 1 = 1 Then":        If Like(s, p) Then msgbox s 'ok
   s = "If fncnam(A, B) Then": If Like(s, p) Then msgbox s 'ok
   s = "If Then":              If Like(s, p) Then msgbox s 'nop
   
End Sub


As you can see it is much more powerful than the Like-operator in VB

happy coding

OlimilO

poetfreak

Beginner

Posts: 2

Date of registration: Dec 3rd 2012

  • Send private message

2

Wednesday, December 5th 2012, 3:43am

RegEx regular expressions function...

I feel like i am right on the cusp on getting how you do the non "basic" part of this. I knew how to do API calls and whatnot back in my VB days, but have been doing php mostly for a while (web design).

So to brush up, I was going to make a css compressor from this code:

function compress($css){
// Remove comments
$css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);

// Remove spaces before and after symbols
$css = preg_replace('/(\s(?=\W))|((?<=\W)\s)/', '', $css);

// Remove remaining whitespace
$css = str_replace(array("\r\n","\r","\n","\t",' ',' ',' '), '', $css);
return $css;
}

But I do not understand quite how to do it. I know the code here is the start of creating preg_replace() because I need RegEx ... wait. Can I even pass an array as a parameter in a function in Jabaco??

Rate this thread
WoltLab Burning Board