lookahead

How to match multiple words in regex

隐身守侯 提交于 2019-11-29 03:41:32
Just a simple regex I don't know how to write. The regex has to make sure a string matches all 3 words. I see how to make it match any of the 3: /advancedbrain|com_ixxocart|p\=completed/ but I need to make sure that all 3 words are present in the string. Here are the words advancebrain com_ixxocart p=completed Use lookahead assertions : ^(?=.*advancebrain)(?=.*com_ixxochart)(?=.*p=completed) will match if all three terms are present. You might want to add \b work boundaries around your search terms to ensure that they are matched as complete words and not substrings of other words (like

negative lookahead assertion not working in python

本小妞迷上赌 提交于 2019-11-29 02:20:39
Task: - given: a list of images filenames - todo: create a new list with filenames not containing the word "thumb" - i.e. only target the non-thumbnail images (with PIL - Python Imaging Library). I've tried r".*(?!thumb).*" but it failed. I've found the solution (here on stackoverflow) to prepend a ^ to the regex and to put the .* into the negative lookahead: r"^(?!.*thumb).*" and this now works. The thing is, I would like to understand why my first solution did not work but I don't. Since regexes are complicated enough, I would really like to understand them. What I do understand is that the

Does regex lookahead affect subsequent match?

♀尐吖头ヾ 提交于 2019-11-28 11:15:57
问题 I was playing around with regular expression look-aheads and came across something I don't understand. I would expect this regular expression: (?=1)x to match this string: "x1" But it doesn't. In ruby the code looks like: > "x1".match /(?=1)x/ => nil Here's what I would expect to happen: We start with the regular expression parser's cursor on "x". The regexp engine searches the string for "1" and gets a match. The cursor is still on "x" The regexp engine searches for "x" and finds it, since

Regular Expression to match only one angle bracket

你。 提交于 2019-11-28 10:59:41
问题 I'm looking for a regular expression that matches the '>' in a > b > b> ... but not two or more angled brackets, i.e. it should not match a>>b >> b>> ... I was sure to do that with lookaheads or lookbehinds, but for some reason neither \>(?!\>) nor (?<!\>)\> work..? Thanks! 回答1: Perl syntax: /(?<!>)>(?!>)/ Without using lookahead or lookbehind: /(?:^|[^>])>(?:[^>]|$)/ 回答2: perreal's first regex is correct. However, the second regex given in that answer subtly fails in one condition. Since it

String negation using regular expressions

痞子三分冷 提交于 2019-11-28 06:45:43
Is it possible to do string negation in regular expressions? I need to match all strings that do not contain the string ".." . I know you can use ^[^\.]*$ to match all strings that do not contain "." but I need to match more than one character. I know I could simply match a string containing ".." and then negate the return value of the match to achieve the same result but I just wondered if it was possible. You can use negative lookaheads: ^(?!.*\.\.).*$ That causes the expression to not match if it can find a sequence of two periods anywhere in the string. ^(?:(?!\.\.).)*$ will only match if

StackOverflowError when matching large input using RegEx

夙愿已清 提交于 2019-11-28 05:24:40
问题 I got StackOverflowError when matching the result using a RegEx pattern. The pattern is (\d\*?(;(?=\d))?)+ . This regex is used to validate the input: 12345;4342;234*;123*;344324 The input is a string consists of values (only digits) separated by ; . Each value could include one * at the end (used as wildcard for other matching). There is no ; at the end of the string. The problem is that this regex works fine which small number of values. But when the numbers of values is too large (over 300

Why is lookahead (sometimes) faster than capturing?

半腔热情 提交于 2019-11-28 03:54:38
问题 This question is inspired by this other one. Comparing s/,(\d)/$1/ to s/,(?=\d)// : the former uses a capture group to replace only the digit but not the comma, the latter uses a lookahead to determine whether the comma is succeeded by a digit. Why is the latter sometimes faster, as discussed in this answer? 回答1: The two approaches do different things and have different kinds of overhead costs. When you capture, perl has to make a copy of the captured text. Look-ahead matches without

Change Password Control RegEx validating oddly in IE 7 only

纵饮孤独 提交于 2019-11-28 02:14:25
I'm using the Asp.net change password control in my application and all seems to be find and dandy until a user tells me she has a problem meeting the strength requirements when changing her password. Looking into this, she is using IE 7 and no matter what she puts in, the validation fails (and ONLY in IE 7. Firefox, IE 8, Chrome etc. all work as expected). Here is the regex i'm using: ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{5,15}$ I've tried out a few others that I've found throughout this site and others that folks seem to be using with no issues and I come across the same problem. It seems

python re.split lookahead pattern

牧云@^-^@ 提交于 2019-11-28 02:10:20
I'm trying re.split to get BCF#, BTS# and LAC, CI from logfile with the header and regular structure inside: ================================================================================== RADIO NETWORK CONFIGURATION IN BSC: E P B F T R C D-CHANNEL BUSY AD OP R ET- BCCH/CBCH/ R E S O&M LINK HR FR LAC CI HOP ST STATE FREQ T PCM ERACH X F U NAME ST /GP ===================== == ====== ==== == ==== =========== = = == ===== == === === BCF-0010 FLEXI MULTI U WO 2 LM10 WO 10090 31335 BTS-0010 U WO 0 0 KHAKHAATT070D BB/- 7 TRX-001 U WO 779 0 1348 MBCCH+CBCH P 0 TRX-002 U WO 659 0 1348 1 TRX-003 U

Javascript won't split using regex

时光总嘲笑我的痴心妄想 提交于 2019-11-27 23:31:30
Since I started writing this question, I think I figured out the answers to every question I had, but I thought I'd post anyway, as it might be useful to others and more clarification might be helpful. I was trying to use a regular expression with lookahead with the javascript function split. For some reason it was not splitting the string even though it finds a match when I call match. I originally thought the problem was from using lookahead in my regular expression. Here is a simplified example: Doesn't work: "aaaaBaaaa".split("(?=B)."); Works: "aaaaBaaaa".match("(?=B)."); It appears the