regex-lookarounds

How to match bold markdown if it isn't preceded with a backslash?

二次信任 提交于 2020-01-16 12:02:14
问题 I'm looking to match bolded markdown. Here are some examples: qwer *asdf* zxcv matches *asdf* qwer*asdf*zxcv matches *asdf* qwer \*asdf* zxcv does not match *qwer* asdf zxcv matches *qwer* A negative look behind like this (?<!\\)\*(.*)\* works. Except there is no browser support in Firefox, so I cannot use it. Similarly, I can get very close with (^|[^\\])\*(.*)\* The issue is that there are two capture groups, and I need the index of the second capture group, and Javascript only returns the

Regular Expressions, understanding lookbehind in combination with the or operator

隐身守侯 提交于 2020-01-11 07:27:46
问题 This is more a question of understanding than an actual problem. The situation explains as follows. I got some float numbers (e.g. an amount of money) between two quotation marks "". Examples: "1,23" "12,23" "123,23" Now I wanted to match the comma in those expressions. I built the following regex which works for me: (?<=\"[0-9]|[0-9]{2})(,)(?=[0-9]{2}\") The part which I don't completly understand is the lookbehind in combination with the or "|". But let's break it up: ( ?<= //Start of the

R tidyr regex: extract ordered numbers from character column

瘦欲@ 提交于 2020-01-04 05:18:07
问题 Suppose I have a data frame like this df <- data.frame(x=c("This script outputs 10 visualizations.", "This script outputs 1 visualization.", "This script outputs 5 data files.", "This script outputs 1 data file.", "This script doesn't output any visualizations or data files", "This script outputs 9 visualizations and 28 data files.", "This script outputs 1 visualization and 1 data file.")) It looks like this x 1 This script outputs 10 visualizations. 2 This script outputs 1 visualization. 3

Regex for Parsing JSON

↘锁芯ラ 提交于 2020-01-03 03:32:06
问题 I have a column of data I'm reading in Tableau directly from Redshift. This column contains a JSON object. It looks like this: {"Age": 58, "City": "Wisconsin Rapids", "Race": "Other", "State": "Wisconsin", "Gender": "Female", "Country": "United States"} I wish to extract this data by generating a column with a calculated field for each data point of interest using Tableau's REGEXP_EXTRACT function. I.e. an Age column, a City column etc. How do I write a line of regular expressions to get the

One-liner to print all lines between two patterns

橙三吉。 提交于 2020-01-01 16:44:08
问题 Using one line of Perl code, what is the shortest way possible to print all the lines between two patterns not including the lines with the patterns? If this is file.txt: aaa START bbb ccc ddd END eee fff I want to print this: bbb ccc ddd I can get most of the way there using something like this: perl -ne 'print if (/^START/../^END/);' That includes the START and END lines, though. I can get the job done like this: perl -ne 'if (/^START/../^END/) { print unless (/^(START)|(END)/); };' file

One-liner to print all lines between two patterns

我只是一个虾纸丫 提交于 2020-01-01 16:39:16
问题 Using one line of Perl code, what is the shortest way possible to print all the lines between two patterns not including the lines with the patterns? If this is file.txt: aaa START bbb ccc ddd END eee fff I want to print this: bbb ccc ddd I can get most of the way there using something like this: perl -ne 'print if (/^START/../^END/);' That includes the START and END lines, though. I can get the job done like this: perl -ne 'if (/^START/../^END/) { print unless (/^(START)|(END)/); };' file

Negative look-ahead assertion in list.files in R

♀尐吖头ヾ 提交于 2020-01-01 04:57:09
问题 I try to list all files in a directory that do not start with "Camera1", but end with ".png". For doing so, I am using a regular expression in list.files in R. To exclude "Camera1", I tried to use a negative lookahead, but it doesn't work. Where is my mistake? ;) list.files(pathToDirectory, pattern = "^(?!Camera1).*\\.png") I get the error: invalid 'pattern' regular expression Thanks in advance :) 回答1: Looks like the default engine doesn't like lookarounds, so you need to use Perl. This works

Why does this backreference not work inside a lookbehind?

假装没事ソ 提交于 2019-12-29 04:34:06
问题 Matching a repeated character in regex is simple with a backreference: (.)\1 Test it here. However, I would like to match the character after the pair of characters, so I thought I could simply put this in a lookbehind: (?<=(.)\1). Unfortunately, this doesn't match anything. Why is that? In other flavours I wouldn't be surprised because there are strong restrictions on lookbehinds, but .NET usually supports arbitrarily complicated patterns inside lookbehinds. 回答1: The short version:

Regex lookahead for 'not followed by' in grep

为君一笑 提交于 2019-12-27 13:36:49
问题 I am attempting to grep for all instances of Ui\. not followed by Line or even just the letter L What is the proper way to write a regex for finding all instances of a particular string NOT followed by another string? Using lookaheads grep "Ui\.(?!L)" * bash: !L: event not found grep "Ui\.(?!(Line))" * nothing 回答1: Negative lookahead, which is what you're after, requires a more powerful tool than the standard grep . You need a PCRE-enabled grep. If you have GNU grep , the current version

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(