StrRegexIsMatch - Forum

Forum Navigation
You need to log in to create posts and topics.

StrRegexIsMatch

I'm trying to find out if the first 5 characters of a variable are numbers only. Every RegEx tester I've used says this will work, yet I keep getting an error.

Suggestions?

SubStr "[Input.PartNo]" "1" "5" "[Input.PartNoIsNumber]"
StrRegexIsMatch "/^[0-9]+$/" "[Input.PartNoIsNum]" "[Input.PartNoIsNumYN]"
SubStr "[Input.PartNo]" "6" "1" "[Input.PartNoIsNumberS]"
IfEx "[Input.PartNoIsNumYN] = [#34]True[#34] AND [Input.PartNoIsNumberS] = [#34]S[#34]"
SetVar "[Input.Standard1]" "Special"
GotoLine "end"
EndIf

 

@pauljonestindall

I have not had an opportunity to test this with VisualNEOWin, but try this Regex Pattern ...

StrRegexIsMatch "/^[0-9]{5,5}/" "[Input.PartNoIsNum]" "[Input.PartNoIsNumYN]"

... {5,5} says there must be between 5 and 5 matching characters ... in the range 0 to 9

I tested cases here ... https://regex101.com/ ... and worked to my satisfaction.

Note that if you pass it a string that has 6 or more characters, 5 matches will give a 'matching result' ... but you are making sure to only pass the first 5 characters.

Here is a non-Regex solution ... a subroutine ...

... tells you if string has EXACTLY 5 digits
... accepts string in [MatchCheck]
... returns result in [MatchResult] (Yes or No or Wrong length)
:DoesItMatch
StrLen "[MatchCheck]" "[MatchCheckLen]"
If "[MatchCheckLen]" "<>" "5"
  SetVar "[MatchResult]" "Wrong Length"
  Return
Endif

SetVar "[MatchResult]" "Yes"
Loop "1" "5" "[thisLoop]"
   SubStr "[MatchCheck]" "[thisLoop]" "1" "[thisMatchChar]"
   IfEx "[thisMatchChar] < 0 OR [thisMatchChar] > 9"
      SetVar "[MatchResult]" "No"
      Return
   EndIf
EndLoop
Return

 

Vadim has reacted to this post.
Vadim

Hi @pauljonestindall @gaev

My plugin acFunctions (with more then 80 actions) has the actions to extract chars. See image gif below:

More plugins: https://acplugins.com/

More info: https://visualneo.com/forum/topic/new-plugin-acfunctions

Regards.

luishp, Vadim and Krakerman have reacted to this post.
luishpVadimKrakerman

@gaev

There's a couple of things going on here... In my post, one of the variables didn't match up but I had already fixed that before you posted.

Second, we both missed the left and right brackets in [0-9]. That should have been [#91]0-9[#93] in the action command.
A couple of things about this one: That got rid of my error message but now returns a false result. I've tested it with several online testers including regex101.com and regextester.com and it works fine. In fact, several variations of this that I found online all work when tested but I still get a false result with VN. Also, most VN native actions, when using the "wizard" box will usually convert special characters like double quotes to [#34] and so on. So I suppose I had a bit of an expectation there that I didn't catch.

I don't think I have ever successfully used the VN RegEx action now that I think about it. I could certainly write a script to do this as you suggested but then what's the point of the RegEx action. I don't want to say that I'm disappointed but I might just have to. I wanted to avoid the script but I guess I'll write it now.

@pauljonestindall

I've tested it with several online testers including regex101.com and regextester.com and it works fine. In fact, several variations of this that I found online all work when tested but I still get a false result with VN.

It most likely has to do with how you specify the RegexPattern ... I know that in javascript, this pattern is an object (not simple text) and you have to create is an object and populate it first ... later this week, I will download the latest version of VisualNEOWin and check it out.

I don't think I have ever successfully used the VN RegEx action now that I think about it.

I believe it was added in the latest (more recent) version(s) of VisualNEOWin.

I could certainly write a script to do this as you suggested but then what's the point of the RegEx action.
I don't want to say that I'm disappointed but I might just have to. I wanted to avoid the script but I guess I'll write it now.

In my earlier post, I included a subroutine that I tested successfully with NeoBook ... note that if you intend to use it frequently, you could transform it into a Function (which allows you to invoke a one line Call from any of your pubs.

@gaev

Thanks. I went ahead and wrote out the code as VN script and made it a function. Maybe when I have more time I'll explore the RegEx feature again.

And by the way, do you or any issue with the function editor taking a long time to open? Is it a VN thing? Is it my computer? I do not remember NB taking so long to open it.