regex-lookarounds

Regular expression for email masking

只愿长相守 提交于 2019-12-04 13:54:24
问题 I am trying to write a regular expression to mask an email address. Example below. input: john.doe@example.en.com output: j*******@e*********.com I have tried the following but I just can't seem to get it working correctly. regex: (?<=.).(?=[^@]\*?@) output:j*******@example.en.com regex: (?<=.).(?=[^@]\*?)(?=[^\.]\*?\.) output:j******************.com Any help would be appreciated. demo 回答1: Update with various masking email solutions foo@bar.com ⇒ f**@b**.com (current question) - s.replaceAll

One-liner to print all lines between two patterns

一曲冷凌霜 提交于 2019-12-04 13:28:38
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.txt But that seems redundant. What I'd really like to do is use lookbehind and lookahead assertions like

Regular expressions negative lookahead

▼魔方 西西 提交于 2019-12-04 04:20:18
I'm doing some regular expression gymnastics. I set myself the task of trying to search for C# code where there is a usage of the as-operator not followed by a null-check within a reasonable amount of space. Now I don't want to parse the C# code. E.g. I want to capture code snippets such as var x1 = x as SimpleRes; var y1 = y as SimpleRes; if(x1.a == y1.a) however, not capture var x1 = x as SimpleRes; var y1 = y as SimpleRes; if(x1 == null) nor for that matter var x1 = x as SimpleRes; var y1 = y as SimpleRes; if(somethingunrelated == null) {...} if(x1.a == y1.a) Thus any random null-check will

Nested regex lookahead and lookbehind

僤鯓⒐⒋嵵緔 提交于 2019-12-04 02:53:44
I am having problems with the nested '+'/'-' lookahead/lookbehind in regex. Let's say that I want to change the '*' in a string with '%' and let's say that '\' escapes the next character. (Turning a regex to sql like command ^^). So the string '*test*' should be changed to '%test%' , '\\*test\\*' -> '\\%test\\%' , but '\*test\*' and '\\\*test\\\*' should stay the same. I tried: (?<!\\)(?=\\\\)*\* but this doesn't work (?<!\\)((?=\\\\)*\*) ... (?<!\\(?=\\\\)*)\* ... (?=(?<!\\)(?=\\\\)*)\* ... What is the correct regex that will match the '*'s in examples given above? What is the difference

Negative look-ahead assertion in list.files in R

浪尽此生 提交于 2019-12-03 12:38:39
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 :) Looks like the default engine doesn't like lookarounds, so you need to use Perl. This works: dat <- c("Camera1.png", "Camera2.png", "hello.png", "boo") grep("^(?!Camera1).*\\.png", dat, value=T, perl

Regular expression for email masking

北城余情 提交于 2019-12-03 08:51:19
I am trying to write a regular expression to mask an email address. Example below. input: john.doe@example.en.com output: j*******@e*********.com I have tried the following but I just can't seem to get it working correctly. regex: (?<=.).(?=[^@]\*?@) output:j*******@example.en.com regex: (?<=.).(?=[^@]\*?)(?=[^\.]\*?\.) output:j******************.com Any help would be appreciated. demo Update with various masking email solutions foo@bar.com ⇒ f**@b**.com (current question) - s.replaceAll("(?<=.)[^@](?=[^@]*?@)|(?:(?<=@.)|(?!^)\\G(?=[^@]*$)).(?=.*\\.)", "*") (see the regex demo ) foo@bar.com ⇒

What do we need Lookahead/Lookbehind Zero Width Assertions for?

泪湿孤枕 提交于 2019-12-02 23:52:18
I've just learned about these two concepts in more detail. I've always been good with RegEx, and it seems I've never seen the need for these 2 zero width assertions. I'm pretty sure I'm wrong, but I do not see why these constructs are needed. Consider this example: Match a 'q' which is not followed by a 'u'. 2 strings will be the input: Iraq quit With negative lookahead, the regex looks like this: q(?!u) Without it, it looks like this: q[^u] For the given input, both of these regex give the same results (i.e. matching Iraq but not quit ) (tested with perl). The same idea applies to lookbehinds

Capture groups match with quantifier Regexp

安稳与你 提交于 2019-12-02 21:17:39
问题 I am newbie in regex world, I need to capture some different types of strings. By the way please suggest more elagant way to capture such strings. n = any positive number(not the same) |n||0||0||0||0| |n||n||0||0||0| |n||n||n||0||0| |n||n||n||n||0| |n||n||n||n||n| I have tried to use such regular expression for capturing first and secodn types of strings ^\|([1-9]+)\|(?:([1-9]+)\|){4}|(?:(0)\|){4}$ Zero should be treated as separate char, I need to capture each number or zero The problem now

Python regex: Following lookahead with quantifier

◇◆丶佛笑我妖孽 提交于 2019-12-02 16:21:20
问题 Just trying to wrap my mind around this question, which occurred to me while I was messing around with positive lookaheads. Does this regex make any sense? foo(?=bar)+ re.match() doesn't return an error, but if there's any sense to the '+' quantifier, I can't figure what it would be. (FWIW, regex101.com gives the error 'The preceding token is not quantifiable' ...) Thanks. /John 回答1: There is no reason to use the + quantifier here. Regular expression lookaheads and lookbehinds don't actually

Splitting digits into groups of threes, from right to left using regular expressions

懵懂的女人 提交于 2019-12-02 15:03:51
问题 I have a string '1234567890' that I want split into groups of threes, starting from right to left, with the left most group ranging from one digit to 3-digits (depending on how many digits are left over) Essentially, it's the same procedure as adding commas to a long number, except, I also want to extract the last three digits as well. I tried using look-arounds but couldn't figure out a way to get the last three digits. string = '1234567890' re.compile(r'\d{1,3}(?=(?:\d{3})+$)') re.findall