Sign in to follow this  
Followers 0
rudi

RegEx synax question

11 posts in this topic

Hi.

This basically is a pure RegEx question, so it's a bit OT here :)

But I know that several RegEx specialists are here, ... so forgive me asking here for the required help >_<

To correct the sevices file of several PCs I want to check an additional entry required for an ERP client:

RegExString="turbo\s+1526/tcp"

will match anything like "turbo <whitespace(s)>1526/tcp"

I'd like to expand that search expression, so that it will match this one (without comment).

And additional in case, IF there is a valid comment behind this statement, but NOT, if something follows that isn't a valid comment.

A valid comment is separated through "<whitespace(s)>#"

Valid entries:

"turbo 1526/tcp"

"turbo 1526/tcp "

"turbo 1526/tcp # some comment"

Invalid entries:

"turbo 1526/tcp#"

"turbo 1526/tcp ;"

"turbo 1526/tcp ; some comment"

"turbo 1526/tcp some comment"

"turbo 1526/tcpsome comment"

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites



Hi rudi,

Just a note on Regular Expressions and AutoIt in case you didn't know.

AutoIt uses the PCRE (Perl Compatible Regular Expressions) engine.

If you are using google for tutorials or just searching for specifics, keep this in mind when looking for expressions or help on expressions.

Some things that may help you along with future questions or concerns:

QuickStart

Tutorials

RegExCoach

Share this post


Link to post
Share on other sites

Hi.

The basics of RegEx are known.

What I fail to "express" is:

Match, if "RegEx1" is the full line

OR

Match, if "RegEx1" & "Whitespace(s)" is found

OR

Match, if "RegEx1" & "Whitespace(s)" & "#" is found.

What I thought of is this, but it doesn't do the job, as at least one whiteblank has to follow:

"turbo\s+1526/tcp\s+[#]*"

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

"turbo\s+1526/tcp(?:\s+#\w.*?|\s*)(?:\z|\r)"

Try that

Edit:

Had a mistake after tcp

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Hi,

I think I've got it using optionals:

(turbo\s+1526/tcp(\s+$|\s+#.+))

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

"turbo\s+1526/tcp\(?:\s+#\w.*?|\s*)(?:\z|\r\n)"

Try that

I missed to mention, that I use filereadtoarray() and examine every line separately.

So this one seems to do the job:

"turbo\s+1526/tcp(\s+#.*?$|\s*$)"

Thanks! Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I missed to mention, that I use filereadtoarray() and examine every line separately.

So this one seems to do the job:

"turbo\s+1526/tcp(\s+#.*?$|\s*$)"

Thanks! Rudi.

I made account for that (every line separate or if you were reading an entire file). \z is the same as $ for PCRE :) ...

Edit:

But what you have looks like it will work as well >_<

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Hi rudi,

Just a note on Regular Expressions and AutoIt in case you didn't know.

AutoIt uses the PCRE (Perl Compatible Regular Expressions) engine.

If you are using google for tutorials or just searching for specifics, keep this in mind when looking for expressions or help on expressions.

Some things that may help you along with future questions or concerns:

QuickStart

Tutorials

RegExCoach

Sorry to go offtopic here, but is this a bot or something as the posts from this "user" is allways so "strict" self help related ?

Edited by jokke

UDF:Crypter a file encrypt / decrypt tool with no need to remember a password again. Based on Caesar cipher using entire ASCII Table.Script's: PixelSearch Helper, quick and simple way to create a PixelSeach.Chatserver - simplified, not so complicated multi-socket server.AutoIT - Firewall, simple example on howto create a firewall with AutoIt.

Share this post


Link to post
Share on other sites

Sorry to go offtopic here, but is this a bot or something as the posts from this "user" is allways so "strict" self help related ?

Which just goes to prove a point with this question. No one ever takes the time to search before opening their yap.

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Which just goes to prove a point with this question. No one ever takes the time to search before opening their yap.

WOW i did not expect that one :)

But i get you'r point.


UDF:Crypter a file encrypt / decrypt tool with no need to remember a password again. Based on Caesar cipher using entire ASCII Table.Script's: PixelSearch Helper, quick and simple way to create a PixelSeach.Chatserver - simplified, not so complicated multi-socket server.AutoIT - Firewall, simple example on howto create a firewall with AutoIt.

Share this post


Link to post
Share on other sites

@SmOke_N

Thanks again for your 1st reply. Without that one I would still be searching for the right RegEx :)

Finally this is what is doing all I need:

"^\s*turbo\s+1526/tcp(\s+#.*$|\s*$)"

Regards, Rudi.

-----

PS: Explanation. Not for you, but for others that might search for RegEx examples: I do remember very well my first weeks with RegEx. I'm still a beginner, but now at least I can read most of these expressions >_<

"^": start of line

"\s*": "\s" whitespace, "*" zero to unlimited times. So "leading whitespaces may occur, but are not required"

"turbo": literally "turbo"

"\s+": as above, "+" means one to unlimited times

"1526/tcp": literally, as above

(one|two): match either "one" or "two"

"\s+": one or more whitespaces

"#": followed by the correct comment sign "#"

".*$": "." any character (except linebreak), zero to unlimited times, "$" up to end of line.

"|": OR

"\s": zero to unlimited whitespaces, "$" up to EOL.

So behind the correct entry there may be whitespaces (they don't harm). If whitespaces occur, the 1st "non-whitespace" has to be "#", after that everything to EOL is fine.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

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  
Followers 0