2

I need to make a regex (.net flavour) that matches only "sometext" in the following strings (the second line has a space at the end):

sometext
sometext 
sometext dsf 2131
sometext-1234

but won't match anything in the following:

sometext  12
sometext 3
sometext2
sometext-1

That is it should match "sometext" only if it is not followed by any number of optional separator characters (like space, "-", "/", ".") and 1 or 2 digits (3 or more digits are fine).

I tried this regex with negative lookahead

sometext($|\W(?!\d{1,2}))

but didn't get too far.

1
  • 1
    What is the expected result for sometext-123.1 or sometext-12.123 or sometext-.-/.123. With $ you test the end of the input. What if the input doesn't end after three digits? Commented Dec 17, 2024 at 9:11

3 Answers 3

1

I would use the following negative lookahead approach:

sometext(?!(?:[/.-]|\s*)\d{1,2}\b)

This pattern says to match:

  • sometext match literal 'sometext'
  • (?! lookahead and negatively assert that
    • (?:[/.-]|\s*) what follows is not a /.- separator or zero or more whitespaces
    • \d{1,2}\b which is then followed by either one or two digits
  • ) end of lookahead

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

Note that this matches sometext 12dfg even though it is followed by a separator and 2 digits as OP said. Maybe better to replace the \b with [^\d] instead?
@Chris The OP gave no such sample data. You might be right or not.
1

Try this one

^sometext(?![\s\-\/\.]?\d{1,2})(?!\d)

Regex explanation:

  1. ^: Matches the start of the string.
  2. sometext: Matches the literal "sometext".
  3. (?![\s-/.]?\d{1,2}): Negative lookahead to ensure the next character(s) aren't: An optional separator ([\s-/.]?) followed by 1–2 digits (\d{1,2}).
  4. (?!\d): Another negative lookahead to prevent a digit immediately following "sometext".

Comments

1

You could make use of \W adding it to the negative lookahead:

sometext(?!\W*\d{1,2}\b)

See a regex 101 demo

Note that \W matches a non word character, which is more than only - / .


If you don't want to match 0 or more separators being one of - / . you might also use:

sometext(?![\s/.-]*\d{1,2}\b)

See another regex 101 demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.