Tim
Tim

Reputation: 99408

Match a line without number followed by "."

Update: I would like to match a line, started with (" followed by a number and then anything except "." . For example

("10 Advanced topics 365" "#382")

is a match, while

("10.1 Approximation Algorithms 365" "#382")

is not a match.

My regex is

^\(\"\d+(?!\.).*?$

but it will match both examples above including the second one. So what am I missing here?

Thanks and regards!

Upvotes: 0

Views: 77

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137557

While it's possible to write a RE that will match such a thing (see manji's answer) I hate such things; they're very hard to comprehend later on. I find it's easier to write an RE to match the case that you don't want, and then make the rest of the logic of the program conditional on that RE not matching. This is virtually always trivial to do.


EDIT: Sometimes you can do better. If we're seeking to distinguish between the types of lines you describe, where good lines don't have a period after the first digit and there's always some text at that point:

("10 Advanced topics 365" "#382")
("10.1 Approximation Algorithms 365" "#382")

Then a regular expression of this form will suffice:

^\("\d+[^.].*

Potentially you might need more to properly match the remainder of the line more precisely (e.g., detecting whether it ends with the right character sequence) but that's separate.

Upvotes: 2

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

Via update:

^\("\d[^.]*$

Try this pattern:

(?m)^(?!.*?\d\.).*$

Upvotes: 1

Related Questions