regex-negation

Regular Expression for comments but not within a “string” / not in another container

这一生的挚爱 提交于 2020-01-04 09:33:50
问题 So I need a regular expression for finding single line and multi line comments, but not in a string. (eg. "my /* string" ) for testing ( # single line, /* & */ multi line): # complete line should be found lorem ipsum # from this to line end /* all three lines should be found */ but not here anymore var x = "this # should not be found" var y = "this /* shouldn't */ match either" var z = "but" & /* this must match */ "_" SO does the syntax display really well; I basically want all the gray text

Regular Expression for comments but not within a “string” / not in another container

邮差的信 提交于 2020-01-04 09:31:08
问题 So I need a regular expression for finding single line and multi line comments, but not in a string. (eg. "my /* string" ) for testing ( # single line, /* & */ multi line): # complete line should be found lorem ipsum # from this to line end /* all three lines should be found */ but not here anymore var x = "this # should not be found" var y = "this /* shouldn't */ match either" var z = "but" & /* this must match */ "_" SO does the syntax display really well; I basically want all the gray text

How to get the inverse of a regular expression?

懵懂的女人 提交于 2020-01-02 15:38:41
问题 Let's say I have a regular expression that works correctly to find all of the URLs in a text file: (http://)([a-zA-Z0-9\/\.])* If what I want is not the URLs but the inverse - all other text except the URLs - is there an easy modification to make to get this? 回答1: If for some reason you need a regex-only solution, try this: ((?<=http://[a-zA-Z0-9\/\.#?/%]+(?=[^a-zA-Z0-9\/\.#?/%]))|\A(?!http://[a-zA-Z0-9\/\.#?/%])).+?((?=http://[a-zA-Z0-9\/\.#?/%])|\Z) I expanded the set of of URL characters a

Regex to match keyword if not enclosed by curly braces

寵の児 提交于 2020-01-01 19:21:12
问题 In a PHP variable I have some text that contains some keywords. These keywords are currently capitalised. I would like them to remain capitalised and be wrapped in curly brackets but once only. I am trying to write upgrade code but each time it runs it wraps the keywords in another set of curly brackets. What REGEX do I need to use to match the keyword alone without also matching it if it is {KEYWORD}. For example, the text variable is: $string = "BLOGNAME has posted COUNT new item(s), TABLE

What is wrong with my regex Pattern to find recurring cycles in Python?

試著忘記壹切 提交于 2020-01-01 14:29:48
问题 I want to match any string that has a recurring cycle. Like in this data: 3333333333333333333333333333333333333333 / 1 digit cycle(3) 1666666666666666666666666666666666666666 / 1 digit cycle(6) 1428571428571428571428571428571428571428 / 6 digit cycle(142857) 1111111111111111111111111111111111111111 / 1 digit cycle(1) 0909090909090909090909090909090909090909 / 2 digit cycle(09) 0834522467546323545411673445234655345222 / no cycle 0769230769230769230769230769230769230769 / 6 digit cycle(769230)

What is wrong with my regex Pattern to find recurring cycles in Python?

為{幸葍}努か 提交于 2020-01-01 14:29:04
问题 I want to match any string that has a recurring cycle. Like in this data: 3333333333333333333333333333333333333333 / 1 digit cycle(3) 1666666666666666666666666666666666666666 / 1 digit cycle(6) 1428571428571428571428571428571428571428 / 6 digit cycle(142857) 1111111111111111111111111111111111111111 / 1 digit cycle(1) 0909090909090909090909090909090909090909 / 2 digit cycle(09) 0834522467546323545411673445234655345222 / no cycle 0769230769230769230769230769230769230769 / 6 digit cycle(769230)

Which would be better non-greedy regex or negated character class?

做~自己de王妃 提交于 2019-12-29 08:04:31
问题 I need to match @anything_here@ from a string @anything_here@dhhhd@shdjhjs@ . So I'd used following regex. ^@.*?@ or ^@[^@]*@ Both way it's work but I would like to know which one would be a better solution. Regex with non-greedy repetition or regex with negated character class? 回答1: Negated character classes should usually be prefered over lazy matching, if possible. If the regex is successful, ^@[^@]*@ can match the content between @ s in a single step, while ^@.*?@ needs to expand for each

Which would be better non-greedy regex or negated character class?

纵饮孤独 提交于 2019-12-29 08:04:17
问题 I need to match @anything_here@ from a string @anything_here@dhhhd@shdjhjs@ . So I'd used following regex. ^@.*?@ or ^@[^@]*@ Both way it's work but I would like to know which one would be a better solution. Regex with non-greedy repetition or regex with negated character class? 回答1: Negated character classes should usually be prefered over lazy matching, if possible. If the regex is successful, ^@[^@]*@ can match the content between @ s in a single step, while ^@.*?@ needs to expand for each

Regex: Matching by exclusion, without look-ahead - is it possible?

情到浓时终转凉″ 提交于 2019-12-27 17:37:13
问题 In some regex flavors, [negative] zero-width assertions (look-ahead/look-behind) are not supported. This makes it extremely difficult (impossible?) to state an exclusion. For example "every line that does not have "foo" on it", like this: ^((?!foo).)*$ Can the same thing be achieved without using look-around at all (complexity and performance concerns set aside for the moment)? 回答1: UPDATE: It fails "with two ff before oo" as @Ciantic pointed out in the comments. ^(f(o[^o]|[^o])|[^f])*$ NOTE:

C# regex for negated character class unless chars are next to one another

喜你入骨 提交于 2019-12-24 23:33:25
问题 I need to match the characters between the innermost set of parentheses in a string, but allowing empty parens such as '()'. As best I can tell some kind of negative lookahead is needed here (and it is completely different than the question for which it is marked as duplicate) An initial version, which does not properly include '()' is: var re = new Regex(@"\(([^()]+)\)"); Some test examples: x (a) y -> a x (a b) y -> a b x (a b c) y -> a b c x (a b() c) y -> a b() c x (a() b() c) y -> a() b(