ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: philbond87 on November 03, 2022, 12:06:48 PM

Title: Simple regex question
Post by: philbond87 on November 03, 2022, 12:06:48 PM
I'm trying to match six digits at the beginning of a string, followed by and underscore (and then some other stuff).

I'm using:

\d{6}_.*

However this seems to be matching six or more digits at the beginning. How can I get it to start the match at the very beginning, so that it doesn't, for example skip the first two digits and match the next six (if the string begins with 8 digits)

Thanks
Title: Re: Simple regex question
Post by: blue-j on November 03, 2022, 01:35:07 PM
My friend, "simple" and "regex" are never to be used in the same sentence!

We use this to help us over here:

https://regex101.com/ (https://regex101.com/)

- J
Title: Re: Simple regex question
Post by: philbond87 on November 03, 2022, 02:39:45 PM
@blue-j,

Ha – so true!

Actually, I use that same site for my testing. Unfortunately it wasn't clear to me how to get the behavior I'm after.

I tried playing around with anchors, thinking the answer lay there, however I wasn't having any success (and I'm afraid I'm not understanding how to use the anchor features).
Title: Re: Simple regex question
Post by: Phil Harvey on November 03, 2022, 03:15:38 PM
This should match from the beginning:  ^\d{6}_.*

However, note that a caret is a special character in Windows commands, and must be escaped by using ^^ (I think).

- Phil
Title: Re: Simple regex question
Post by: philbond87 on November 03, 2022, 03:47:46 PM
Yep, that did it.
Thanks Phil!
Title: Re: Simple regex question
Post by: StarGeek on November 04, 2022, 12:20:50 AM
Quote from: Phil Harvey on November 03, 2022, 03:15:38 PMHowever, note that a caret is a special character in Windows commands, and must be escaped by using ^^ (I think).

Using quotes around it will treat it as a normal character.  And in most cases, any regex will likely have quotes around it.

The
-TAG^=
construct is pretty much the only case that needs to be treated specifically with doubling or with quotes that I can think of and that is in CMD only, not PowerShell.