简体   繁体   中英

Regex to match everything except strings that starts with digits

Hi This is my very first question and I'm quite new to regex Im hoping someone could help.

My sample data is as follows:

black PENS Assorted Bit Set 10Pcs 
Hollow blocks 10-16mm Cd3 
Hollow blocks 16-23mm Cd3 
Hollow blocks 5-10mm Cd3 
Hollow blocks To 1-5mm Cd3 

\b[^\d]+\b    

I am trying to come up with a regex that would match everything on the list except words that starts with digits (ex. 10-16mm). I came up with the above regex but unfortunately not the right output that I would like to. CD3 should be matched as it starts it letters and the dash(-) in between the numbers should not.

Appreciate the help!

I would use this pattern:

(?<=^|\s)[^0-9].*?(?=$|\s)

Rather than using word boundaries, I use lookarounds instead. These lookarounds assert that a word ends if what precedes is whitespace or the start of the input, and what proceeds is either whitespace or the end of the input. The reason for this is that using \\b might fail for terms such as a10-333 .

Demo

You may try this:

/\b[^\d-\s][\w-]+\b/i

Where:
\\b - start and end of word
[^\\d-\\s] - match any character except digit, dash and space
[\\w-]+ - match characters from a to z, from 0 to 9 and _ (case insensitive)

You may check this regex here

You can do:

/\b([a-zA-Z]\w+)/

Demo

If you want matches for words such as a33-45 you need to change to:

/\b([a-zA-Z][\w\d-]+)/

Demo 2

Just add to the second character class, [\\w\\d-] what you think is an acceptable 'word' character and add to the first character class, [a-zA-Z] what you think is an acceptable word start character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM