Jump to content

Regex help request


Recommended Posts

Hi, all

I'm trying to figure out how to regex a string like Software.Log.W01D01.Extended.Info.txt to only show text up to the W01D01 and to substitute the periods with spaces. So far, I have been able to capture the text up to the W01D01. On a regex play website, I found to not select a character, use ^\.+ or at least it seems to work when I'm playing with the code.

Here is a test code:

#include "Array.au3"

$varTempInfo = "Software.Log.W01D01.Extended.Info.txt"
$varNameFormattedForSearch = StringRegExp($varTempInfo, ".+?(?=[W]\d\d[D]\d\d)", 1)
_ArrayDisplay($varNameFormattedForSearch)

The best attempt I have made so far is this:

(^\.+)?.+?(?=[W]\d\d[D]\d\d)|^\.+

Other failed attempts are:

.+?(?=[W]\d\d[D]\d\d)|($&[.])
.+?(?=[W]\d\d[D]\d\d)?($&[.])
($&[.])|(.+?(?=[W]\d\d[D]\d\d))
.+?(($&[.])?=[W]\d\d[D]\d\d)

Does anyone know how to do it right? Thanks!

Link to comment
Share on other sites

Could you explain the expected result please? It sounds like you want just the portion that says "W01D01", but you also mention turning periods into spaces.

To capture the W##D## portion, you can use this: (W\d{2}D\d{2})

Also realize that once you head too far down the road of Regular Expressions, you'll eventually realize that it should be split into multiple steps. Beware that time.

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

2 hours ago, abberration said:

.... On a regex play website, I found to not select a character, use ^\.+ or at least it seems to work when I'm playing with the code.

....

 

The PCRE (Perl Compatible Regular Expressions) engine flavour used with AutoIt, "^\.+" will match one or all ("+") of the consecutive literal dots ("\.") at the beginning of the test string ("^").

Local $varTempInfo = "Software.Log.W01D01.Extended.Info.txt"
Local $varNameFormattedForSearch = StringRegExpReplace($varTempInfo, "(\.W[\w]{5}+.+$)|\.", " ")

ConsoleWrite($varNameFormattedForSearch & @CRLF) ; Returns:- Software Log

 

Edited by Malkey
Link to comment
Share on other sites

2 hours ago, seadoggie01 said:

Could you explain the expected result please? It sounds like you want just the portion that says "W01D01", but you also mention turning periods into spaces.

To capture the W##D## portion, you can use this: (W\d{2}D\d{2}) 

Also realize that once you head too far down the road of Regular Expressions, you'll eventually realize that it should be split into multiple steps. Beware that time.

On the example I gave, I want to see the following text from regex (not including the quotes):

"Software Log "

I like your more condensed version using {2} because in the future I may run into instances where I have 4 or more numbers. And splitting it into multiple steps is what I did for a quick fix. I just thought it might be possible to do two things (get all the text to the left of W01D01 and eliminate all periods from that selected text) at the same time - a learning experience, maybe. Thanks for the improved code. I will make note of it.

1 hour ago, Malkey said:

The PCRE (Perl Compatible Regular Expressions) engine flavour used with AutoIt, "^\.+" will match one or all ("+") of the consecutive literal dots ("\.") at the beginning of the test string ("^").

Local $varTempInfo = "Software.Log.W01D01.Extended.Info.txt"
Local $varNameFormattedForSearch = StringRegExpReplace($varTempInfo, "(\.W[\w]{5}+.+$)|\.", " ")

ConsoleWrite($varNameFormattedForSearch & @CRLF) ; Returns:- Software Log

 

I'm impressed, your code works, but I am not well versed in regex yet, so I don't know how it works. I will be researching every part of your code tomorrow night. Thank you for working on this. I really appreciate it!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...