Regex woes! Help needed.

Associate
Joined
17 Nov 2003
Posts
1,945
Location
Gateshead
Why? I don't know, but I've never been able to wrap my head around regular expressions. Now I'm stuck, not even sure it's possible, but it probably is :)

I need to test a string for 'test'. Simple enough. This 'test' must also be at the start of the string. Still simple enough. 'test' at the start of the string must NOT be followed by ' -' (space hyphen), this is where I'm stuck.

eg...

test blahblahblah - pass
test - blahblahblah - fail, followed by ' -'
a test blahblahblah - fail, not start of string.
a test - blahblahblah - fail, not start of string and followed by ' -'

Note: blahblahblah will be a random number and selection of characters but 'test' will always be 'test'.

Ultimately, I need to rename millions of files of the format 'test blahblahblah' to 'test - blahblahblah' without renaming them 'test - - blahblahblah'. Hope that make sense??

Anyone good with regex help me out?

Cheers !
 
If it's always followed by a number, it would be easier to just match that rather than trying to not match the dash.

something like:
"^test[0-9]"
^ matches from the start of the string
test matches the word test
[0-9] matches any digit

Use this site to build and test it https://regex101.com/
 
Sorry, when I said 'a random number and selection of characters' I perhaps wasn't clear, it'll be a mix of digits and characters.

You have got me thinking though as blahblahblah will always be alphanumeric.

Hmmm???
 
A developer had one problem he decided to solve by using a regular expression.

Now he has two problems.
 
A developer had one problem he decided to solve by using a regular expression.

Now he has two problems.

I believe that!!

It is very powerful once I understand and I'm finally getting my head around it. I really should have learned this years ago, it's just so foreign! :)
 
Back
Top Bottom