Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

python - ignore first occurance of letter regex

using the following strings

    9989S90K72MF-1
    9989S90S-1
    9989S75K60MF-1
    9989S75S-1

I Would like to extract the below from those strings.

    9989S90
    9989S90
    9989S75
    9989S75

So far I have:

(^.*?(?=K|-))

Which gives me:

    9989S90
    9989S90S
    9989S75
    9989S75S

Here's a link https://regex101.com/r/d1nQj0/1

I've tried a few different regex but can't seem to nail it. Is there a way to ignore the first occurrence of a digit/letter? Which in my case would be S

question from:https://stackoverflow.com/questions/65878684/ignore-first-occurance-of-letter-regex

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following regex matches a string at the beginning of a line that contains a single S up to but not including the first occurrence of S or K

^(.*?S.*?)(?=K|S)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...