Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

RegEx and ChatGPT (via Bing Chat)

Aug
1,929
70
I never really understood how to use RegEx, but saw others using it to make their code filtering stronger.

I asked ChatGPT (via Bing Chat) to provide me with a RegEx to show me if a line begins with ECHO ON or ECHO OFF, and that the line may or may not begin with @.

It returned the following;
To match the same, except the line may or may not begin with @, you can use the following regex expression:

^(@?)(ECHO OFF|ECHO ON)

This expression means:

  • ^ matches the beginning of the line.
  • (@?) matches zero or one occurrence of @ as a group.
  • (ECHO OFF|ECHO ON) matches either ECHO OFF or ECHO ON as a group.
  • The parentheses () are used to group the alternatives.
  • The vertical bar | is used to separate the alternatives.
  • The question mark ? is used to indicate that the preceding character or group is optional.
This expression will match lines that start with ECHO OFF or ECHO ON, with or without @, such as:

ECHO OFF ECHO ON ECHO OFF REM This is a comment ECHO ON ECHO Hello world @ECHO OFF @ECHO ON @ECHO OFF REM This is a comment @ECHO ON ECHO Hello world

But it will not match lines that do not start with ECHO OFF or ECHO ON, with or without @, such as:

REM ECHO OFF ECHO Hello world ECHO is off ECHO is on @REM ECHO OFF @ECHO Hello world @ECHO is off @ECHO is on

The break down of what each item did made understanding it much easier.

If you are using a stand-alone TCC, press Ctrl-F7 to launch the Regular Express Analyzer to test your Expression.

Just passing this on, in the event others may want to try this, in order to generate a RegEx expression for their code.

Joe

Ref: Bing AI - Search
Ref: Take Command Tools Menu
 
I think "@*" is the more common way to indicate zero or more occurrences of "@" (or any single character). You can use "OFF|ON" (positioned correctly). Throw in (?i) and you'll get case insensitivity. Below, the "^" must be outside the quotes; I don't know why (and I'd like to know why).

Code:
v:\> echo %@regex[^"(?i)@*echo (off|on)","EcHo OfF"]
1

v:\> echo %@regex[^"(?i)@*echo (off|on)","EcHo On"]
1

v:\> echo %@regex[^"(?i)@*echo (off|on)","@EcHo On"]
1

v:\> echo %@regex[^"(?i)@*echo (off|on)","x@EcHo On"]
0

v:\> echo %@regex[^"(?i)@*echo (off|on)","EcHo Of"]
0
 
I think I figured out the "^" thing. TCC does not strip surrounding quotes from the string and does strip surrounding quotes from the regex.

This works because TCC doesn't strip the quotes in the regex (they don't surround) ... so they match the quotes in the string.

Code:
v:\> echo %@regex[^"(?i)@*echo","echo"]
1

And this works because TCC does strip the quotes in the regex.

Code:
v:\> echo %@regex["(?i)^@*echo",echo]
1

This fails because TCC doesn't strip the quotes in the regex (they don't surround) and they're not found in the string.

Code:
v:\> echo %@regex[^"(?i)@*echo",echo]
0

And this fails because TCC strips the quotes in the regex but the string starts with a quote.

Code:
v:\> echo %@regex["^(?i)@*echo","echo"]
0
 
"zero-or-more" is not the same as "zero-or-one".
Caret is the default escape symbol. Strange that you did not get a "unmatched quotation" error.
 
"zero-or-more" is not the same as "zero-or-one".
Caret is the default escape symbol. Strange that you did not get a "unmatched quotation" error.
Why do you mention "zero-or-one"?

I agree. But @REGEX seems to be an exception, possibly because '^' is regex-special.

Code:
v:\> echo %@len[^"foo",foo]
TCC: Syntax error "@len[^"foo",foo]"

v:\> echo %@regex[^"foo",foo]
0

@REREPLACE does not seem to be an exception.

Code:
v:\> echo %@rereplace[^"foo",bar,foo]
TCC: Syntax error "@rereplace[^"foo",bar,foo]"

And I think "TCC: No closing quote" only comes from backticks (`).
 
I think TCC is inconsistent in quoting much more than CMD is. Part of the reason I'm stepping away from using it.
 
Back
Top