Jump to content
Sign in to follow this  
JScript

Question about Regex...

Recommended Posts

JScript

Hello guys!

I have a doubt that for many can be quite simple to solve, but unfortunately I do not understand almost nothing of regex!

I wanted to know how to identify only the character _ at the end of the line indicating that it continues in the next line...

Example:

MsgBox(4096, "Title _ v1", "This program is free software: you can redistribute it and/or modify" & _
        'it under the terms of the GNU General Public License " _"' & _ ; Only comment!
        "as published by the Free Software Foundation, either version 3" & _
        "of the License, or (at your option) any later version.")

If anyone can help me I'll be very grateful!

Edit: Another example:

$aRtl = DllCall("ntdll.dll", "int", "RtlCompressBuffer", _
        "ushort", 2, _ ;------> CompressFormat
        "ptr", $pInput, _ ;---> ptrSrceBuffer
        "dword", $iSize, _ ;--> SrceBufferSize
        "ptr", $pBuffer, _ ;--> ptrDestBuffer
        "dword", $iBufLen, _ ;> DestBufferSize
        "dword", 4096, _ ;----> ChunkSize
        "dword*", 0, _ ;------> CompressedSize
        "ptr", $pWrkSp) ;-----> WorkspaceBuffer

thankful,

JS

Edited by JScript
  • Like 1

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Share this post


Link to post
Share on other sites
BrewManNH

Look for a "_" followed by a CRLF.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
BrewManNH

Two vastly different questions, if you have alternate requirements, then you should have stated them up front.

You can use the pipe as an OR statement to see if the underscore is followed by a space and either a CRLF or a semi-colon.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
iamtheky

why not just regexp for "& _"

$Aunderscores = StringRegExp ($string , "& _", 3)

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
czardas

This isn't quite working and I don't know why. I decided to post my attemps anyway, in case it turns out to be useful and that I might also learn something.

#include <Array.au3>

$sString = "This program is free software: you can redistribute it and/or modify & _" & @CRLF & _
        'it under the terms of the GNU General Public License " _" & _ ; Only comment!' & @CRLF & _ ; Only comment!
        "as published by the Free Software Foundation, either version 3 & _" & @CRLF & _
        "of the License, or (at your option) any later version."

$regExp = "(&h+_h*(;.*)?)"

$aArray = StringRegExp($sString, $regExp, 3)

_ArrayDisplay($aArray)

Anyway I believe it is customary to strip away all comments before parsing. I would just concatenate strings ending with underscore using StringReplace.

Edit

I believe the above RegExp approach is not suitable in most circumstances. It is inefficient and my example (besides being broken) does not cater for matches found within strings. There may be simpler ways than using RegExp, depending on what you want to achieve.

Edited by czardas
  • Like 1

Share this post


Link to post
Share on other sites
JScript

Two vastly different questions, if you have alternate requirements, then you should have stated them up front.

I guess you did not pay attention to the example I posted!

MsgBox(4096, "Title _ v1", "This program is free software: you can redistribute it and/or modify" & _
        'it under the terms of the GNU General Public License " _"' & _ ; Only comment!
        "as published by the Free Software Foundation, either version 3" & _
        "of the License, or (at your option) any later version.")

You would have seen this sequence: " _"' & _ ; Only comment!

And would not have given this answer:

Look for a "_" followed by a CRLF.

@boththose

Sorry, but sometimes can be as follows: , _

@czardas

Excellent, but as I said above, sometimes can be as follows: , _

@scullion

That phrase / advice was for me? If yes, thank you!

JS

Edited by JScript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Share this post


Link to post
Share on other sites
PhoenixXL

This might help

Func MatchSingleScores($nString)
Local $nPattern = "['" & '"]' & ".*" & "['" & '"]'

;~ ConsoleWrite($nPattern & @CR)

;Remove String enclosed in Quotes
Local $nRegEx = StringRegExpReplace($nString, $nPattern, '')
If @error Then Exit @extended

;Remove Comments
$nPattern = ";.*"
$nRegEx = StringRegExpReplace($nRegEx, $nPattern, '')

;Return Match
Return StringRegExp($nRegEx,' _')
EndFunc ;==>ReplaceScores

If MatchSingleScores(' "Hello there" _ ;This is a comment' & @CRLF _
& ' "This is the Second Line"') Then Exit ConsoleWrite('Underscore is There!'&@CR)=0

Exit ConsoleWrite('No Match Found')=0

Edited by PhoenixXL
  • Like 1

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
Varian

This captures each line that ends with an underscore or ends with an underscore & a comment; This returns the entire matched line up to the underscore

#include <Array.au3>

Local $iString
$iString &= 'MsgBox(4096, "Title _ v1", "This program is free software: you can redistribute it and/or modify" & _' & @LF
$iString &= ' "it under the terms of the GNU General Public License " _" & _ ; Only comment!' & @LF
$iString &= ' "as published by the Free Software Foundation, either version 3" & _' & @LF
$iString &= ' "of the License, or (at your option) any later version.")' & @LF
$iString &= '$aRtl = DllCall("ntdll.dll", "int", "RtlCompressBuffer", _' & @LF
$iString &= ' "ushort", 2, _ ;------> CompressFormat' & @LF
$iString &= ' "ptr", $pInput, _ ;---> ptrSrceBuffer' & @LF
$iString &= ' "dword", $iSize, _ ;--> SrceBufferSize' & @LF
$iString &= ' "ptr", $pBuffer, _ ;--> ptrDestBuffer' & @LF
$iString &= ' "dword", $iBufLen, _ ;> DestBufferSize' & @LF
$iString &= ' "dword", 4096, _ ;----> ChunkSize' & @LF
$iString &= ' "dword*", 0, _ ;------> CompressedSize' & @LF
$iString &= ' "ptr", $pWrkSp) ;-----> WorkspaceBuffer' & @LF
$iString &= '_ ; Only comment!'
MsgBox(262208, 'Original String', $iString)
RegEx($iString)

Func RegEx($nString)
Local $nPattern = "(?m)(^.*_)(?:s*$|s*;.*s*$)"
ConsoleWrite($nPattern & @CR)
Local $nRegex = StringRegExp($nString, $nPattern, 3)
If @error Then Exit @extended
_ArrayDisplay($nRegex)
EndFunc ;==>RegEx
Edited by Varian
  • Like 1

Share this post


Link to post
Share on other sites
jchd

Detecting AutoIt statement continuation requires parsing from the left hand and implies actual parsing of the syntax.

Ad hoc patterns can do it in a restricted number of cases, but certainly not in all cases.

I don't believe a simple regexp can be produced to this effect and, should one ever pop up, it would be fairly complex and unmaintainable.

This is a job for a lexer.

  • Like 1

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
JScript

@PhoenixXL

Very nice, thank you!!!

@Varian

Of all the tests I did was his example was what went right :thumbsup:

but as was said in the post above, I believe that may fail in some cases, although it has not failed with me!

@jchd

You hit the point...

I thank everyone for their efforts in helping me!

JS

Edited by JScript
  • Like 1

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Share this post


Link to post
Share on other sites
BrewManNH

identify only the character _ at the end of the line

From your description of what you were looking for in the first post, and me not looking at the whole string, you were asking how to identify the underscore at the end of the line. Having a further requirement to identify a comment at the end of the line with an underscore before it isn't in the description of what you were looking for. As I said, explain the full objective upfront and it won't require you having to refine it as the thread continues. A line with an underscore at the end IS vastly different from a continued line with a comment at the end.Just my 2 cents.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
PhoenixXL

modified a bit varian's code

-->Now Replaces the Underscores with the Next Line

Local $iString
$iString = 'DllCall("ntdll.dll", "int", "RtlCompressBuffer", _' & @LF & _
'"ushort", 2, _ ;------> CompressFormat' & @LF & _
'"ptr", $pInput, _ ;---> ptrSrceBuffer' & @LF & _
'"dword", $iSize, _ ;--> SrceBufferSize' & @LF & _
'"ptr", $pBuffer, _ ;--> ptrDestBuffer' & @LF & _
'"dword", $iBufLen, _ ;> DestBufferSize' & @LF & _
'"dword", 4096, _ ;----> ChunkSize' & @LF & _
'"dword*", 0, _ ;------> CompressedSize' & @LF & _
'"ptr", $pWrkSp) ;-----> WorkspaceBuffer'

ConsoleWrite('Returned: ' & RegEx($iString) & @CR)

;Replaces the Underscores with the NextLine
Func RegEx($sString)
Local $sPattern = "(?m)(^.*)( _)(?:s*$|s*;.*s*$)"
;Remove the Comments
Local $sRegex = StringRegExpReplace($sString, $sPattern, '1')
;Now Lets Join them
Return StringRegExpReplace($sRegex, '(?m)([rn])', '')
EndFunc   ;==>RegEx

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
jchd

As I said, that simple-minded approach fails if the substring ' _' is present in either a literal string in the active statement part or in a mono-line comment.

The general case needs a lexer. Period.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
PhoenixXL

As I said, that simple-minded approach fails if the substring ' _' is present in either a literal string in the active statement part or in a mono-line comment.

Can you please give a correct Autoit Syntax where this fails

Thanks for your time


My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
jchd

Here you are:

Local $sString
$sString = '$s = "This a short example."' & @CRLF & _
'ConsoleWrite($s & @LF)'
ConsoleWrite('Returned: ' & RegEx($sString) & @CR)

$sString = '$s = "Another example." ; with a short comment _' & @CRLF & _
'ConsoleWrite($s & @LF)'
ConsoleWrite('Returned: ' & RegEx($sString) & @CR)

$sString = '$s = "Yet another example _ ; tada."' & @CRLF & _
'ConsoleWrite($s & @LF)'
ConsoleWrite('Returned: ' & RegEx($sString) & @CR)

;Replaces the Underscores with the NextLine
Func RegEx($sString)
Local $sPattern = "(?m)(^.*)( _)(?:s*$|s*;.*s*$)"
;Remove the Comments
Local $sRegex = StringRegExpReplace($sString, $sPattern, '1')
;Now Lets Join them
Return StringRegExpReplace($sRegex, '(?m)([rn])', '')
EndFunc ;==>RegEx

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
PhoenixXL

right its very complex :oops:


My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
jchd

Please don't take it bad.

The issue here is that we can have a number of cases, the most delicate being that we need to decide if we're in a string literal, a true continuation operator or inside a comment. We may encounter the ' _' sequence everywhere, or not at all.

To make things harder, a string can use single- or double-quotes delimiters ('abc' = "abc"), yet include double- or single-quotes respectively, or escaped (doubled) quote delimiter, non exclusively ("Mac'Do" = 'Mac''Do', 'Hi "folks"' = "Hi ""folks"""). That only by itself is far from completely trivial. But realize that what looks like a valid string literal with any quoting might in fact be inside a comment, as a line by itself or after a partial or final statement.

The only way out is to parse the syntax from the left hand and that requires a gross-grain parser. I fully acknowledge this _can_ in theory be done in a single PCRE regexp (after all PCRE is as close to a Turing machine as one can dream). BUT --and this is a big but-- doing so is very far from elementary and would be both ugly, very large and unmaintainable.

  • Like 1

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×