4

I'm having quite some trouble to define a regEx that I'm needing....

Basically the idea is to detect all lines that end with a , or a ; character. For this I have defined the following regex:

(,|;)$

Which works fine for this, but then I have the exception that if there's a * character within that line (not necessarily starting with, but at some position), then I don't want to detect that match. Based on this sample:

/**
 * Here there's a comment I don't want to find,
 * but after this comment I do
 */
detectMe;
other,

I would intend to find 2 groups, the first one

/**
 * Here there's a comment I don't want to find,
 * but after this comment I do
 */
detectMe;

And the second one

other,

I've tried many things such as non capturing groups, negative looks ahead and also start of a string with [^\s*\*] with no success. Is there a way to do this?

Some of the regEx I've tried...

^[^\*](.*?)(,|;)$
^[^\s*\*](.*?)(,|;)$
Nissa
  • 4,636
  • 8
  • 29
  • 37
Rodrigo Martinez
  • 913
  • 3
  • 13
  • 29
  • 4
    remove the `(.*?)` that is too broad and use `^([^*\r\n]*)[,;]$` to exclude `*` from the whole line. – Casimir et Hippolyte Oct 04 '18 at 18:51
  • Also note that Javascript has two string methods: `String.prototype.endsWith()` and `String.prototype.includes()`. – Casimir et Hippolyte Oct 04 '18 at 18:56
  • 2
    You may find it useful to read up on parsers and lexical analysis if you're going to be interpreting bodies of text (namely code). Here's a great introduction: https://medium.com/dailyjs/gentle-introduction-into-compilers-part-1-lexical-analysis-and-scanner-733246be6738 – coreyward Oct 04 '18 at 18:57
  • 2
    Do you want to match an optional C comment and the following line ending with `;` or `,`? See https://regex101.com/r/rWmSGj/1. – Wiktor Stribiżew Oct 04 '18 at 19:59
  • @WiktorStribiżew This is exactly what I needed! could you post it as an answer so I can mark it and give up vote? Works like a charm! - Now I'm trying to break it up and understand it – Rodrigo Martinez Oct 04 '18 at 20:12
  • See [my answer](https://stackoverflow.com/a/52654533/3832970) below. You will find the explanation there and helpful links. – Wiktor Stribiżew Oct 04 '18 at 20:21
  • Another idea: [`/^[ \t]*(?!\/?\*)\S.*[;,]$/gm`](https://regex101.com/r/eJYiZY/1) – bobble bubble Oct 04 '18 at 21:05

2 Answers2

1

To match an optional C comment and the following line ending with ; or , you may use

/(?:\/\*+[^*]*\*+(?:[^\/*][^*]*\*+)*\/\r?\n)?.*[;,]$/gm

See this regex demo

Details

  • (?:\/\*+[^*]*\*+(?:[^\/*][^*]*\*+)*\/\r?\n)? - an optional (as there is a ? quantifier after the group) non-capturing group matching 1 or 0 occurrences of
    • \/\*+[^*]*\*+(?:[^\/*][^*]*\*+)*\/ - a C comment pattern
    • \r?\n - a CRLF or LF ending
  • .*[;,]$ - a whole line that ends with ; or , ($ is the end of a line anchor here due to m modifier).
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

You can use this regex:

/^[^*]*?[,;]$/gm

It will start by mathing any number of characters not being '*', then match ',' or ';' at the end of the line. It uses the global and multiline flags to match all lines.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57
  • This is almost what I needed! indeed, its matching the proper lines, the problem is that now the comment is not included over the first match, its discarded instead. Like this I get 'detectMe;' instead of '/** * Here there's a comment I don't want to find, * but after this comment I do */ detectMe;' – Rodrigo Martinez Oct 04 '18 at 19:36
  • Well, you say, you don't want to find them. Doesn't that mean, that they should not match? – Poul Bak Oct 04 '18 at 19:42