Simple regex question

Started by philbond87, November 03, 2022, 12:06:48 PM

Previous topic - Next topic

philbond87

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

blue-j

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/

- J

philbond87

@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).

Phil Harvey

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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

philbond87


StarGeek

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.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype