w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Python 3: Regex matching 2 seperate conditions
import io import re import sys file = io.StringIO(''' title|Head1|Head2|Head3|head4 ----|------|-----|-----| 1|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None) 2|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None) 3|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None) 4|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None) 5|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None) All|processes:|MemAlloc|=|408125440|(None, None)|0.0.0.0 |(None, None) 0.0.0.0 ,text ''') sys.stdout.writelines(line for line in file if re.match('d+|', line))

Categories : Regex

PHP Regex, Matching anything between two specific words/tags with conditions
Though I agree with the comments on your post, I will give the solution. If you wanted to replace the very/unique.key by something else, the correct regex would look something like this #<table(.*)>((.*)very/unique.key(.*))</table>#imsU The key here is to use the correct modifiers to make it work with your input string. FOr more information on these modifiers, see http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php Now here's an example where I replace the very/unique.key by "foobar" <?php $string = " <table ....> (bunch of content) </table> <table ....> (bunch of content) </table> <table ....> bunch of content very/unique.key </table> <table ....> (bunch of content) </table> <table ....&g

Categories : PHP

Python RegEx query missing overlapping substrings
From the Python 3 documentation (emphasis added): $ python3 -c 'import re; help(re.findall)' Help on function findall in module re: findall(pattern, string, flags=0) Return a list of all non-overlapping matches in the string. If one or more capturing groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result. If you want overlapping instances, use regex.search() in a loop. You have to compile the regular expression because the API for non-compiled regular expressions doesn't take a parameter to specify the starting position. def findall_overlapping(pattern, string, flags=0): """Find all matches, even ones that overlap.""" regex = re.compile(pa

Categories : Regex

matching list of substrings to a list of strings in Python
As described in this answer, a regular expression would be the way to go, since these are modeled as a DFA that can check for all substrings at the same time. You should probably read that answer, as it is quite in-depth.

Categories : Python

Matching "..." = "..." in Python via Regex
Move the quotation marks outside the capture group, if you don't want them to be part of your matches: >>> matches = re.findall(r'"([^"]*)".=."([^"]*)"', line) >>> matches [('string1', 'string2')] Also, since you have space around your "=", you should just match a space. A dot "." matches any character.

Categories : Python

Flip matching substrings
Use custom function for replace: your_string.replace(/([a-g]+)/g, function(all, word) { return word.split("").reverse().join(""); }); Example: > "sgsadebfdzgbd".replace(/([a-g]+)/g, function(all, word) { return word.split("").reverse().join(""); }); "sgsdfbedazdbg" You can also use this shorter, but (in my opinion) less clear version: your_string.replace(/[a-g]+/g, function(word) { return word.split("").reverse().join(""); });

Categories : Javascript

Regex matching between words python
>>> text = '''ip=1.0.8.0 statistic=rtt.std_dev predictions=iad-mci:114.717204,ord-cgnt:30.107700,nyc-inap:32.537077,iad-cgnt:0.000000,hkg-pccw:98.157281,ord-tata:6.058292,sjc-l3:57.089664,nyc-cgnt:36.489616,pvg-cu2:1039.978803,bgl-rel:115.671650,nyc-bgp:94.454690,pvg-cu1:377.429628,las-level3:0.000000,nyc-tgl:119.197070,atl-inap:42.021698 ip=1.0.8.0 statistic=rtt.match_length predictions=iad-mci:13.000000,ord-cgnt:16.000000,nyc-inap:16.000000,iad-cgnt:20.000000,hkg-pccw:16.000000,ord-tata:16.000000,sjc-l3:16.000000,nyc-cgnt:16.000000,pvg-cu2:16.000000,bgl-rel:13.000000,nyc-bgp:16.000000,pvg-cu1:16.000000,las-level3:20.000000,nyc-tgl:16.000000,atl-inap:16.000000 ip=1.0.8.0 statistic=rtt.mean predictions=iad-mci:348.247084,ord-cgnt:319.301775,nyc-inap:328.353336,iad-cgnt:248.60000

Categories : Python

matching but not including one or the other with python regex
>>> import re >>> re.match('<Category:s(w+)', "<Category: XXX</b>").group(1) 'XXX' >>> re.match('<Category:s(w+)', "<Category: XXX &nbsp;-&nbsp;").group(1) 'XXX' or using findall: >>> import re >>> re.findall('<Category:s(w+)', "<Category: XXX &nbsp;-&nbsp;")[0] 'XXX' >>> re.findall('<Category:s(w+)', "<Category: XXX</b>")[0] 'XXX' s matches any whitespace character. w matches any non-alphanumeric character; this is equivalent to the set [^a-zA-Z0-9_]. w+ matches one or more any non-alphanumeric characters. (...) is a capturing group: Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the contents of a group can be

Categories : Python

Regex matching string python
Just escape it using : print re.sub(r'..*?','', strings) # prints iamcoolend Also, you made a mistake - the second . should not be escaped.

Categories : Python

Regex for matching string Python
Something like this: >>> import re >>> strs = """1,000 metric tonnes per contract month Five cents ($0.05) per tonne Five cents ($0.05) per tonne 1,000 metric tonnes per contract month""" >>> [m.group(0) for m in re.finditer(r'$?d+([,.]d+)?', strs)] ['1,000', '$0.05', '$0.05', '1,000'] Demo : http://rubular.com/r/UomzIY3SD3

Categories : Python

Python Regex: Matching from end of string (reverse)
I'm not sure from your example if you're just trying to get the last two parts of the domain name, or if you're trying to remove the numbers. If you just want the last parts of the domain, you can do something like: for i in my_string.split(): '.'.join(i.split('.')[-2:]) This: splits each string into a list of words, split where the '.' was originally, then combines the final two words into a single string, with a '.' separator. Or, like this: >>> my_string = ['123.domain.com', '123.456.domain.com', 'domain.com', 'www.stackoverflow.com'] >>> ['.'.join(i.split('.')[-2:]) for i in my_string] ['domain.com', 'domain.com', 'domain.com', 'stackoverflow.com']

Categories : Python

Double letter matching using python regex
Maybe like this? Seems to work for me. r".*([a-z])1.*((?=(?!1))[a-z])2.*((?=(?!1))(?=(?!2))[a-z])3.*" (?=expr) is a non-consuming regular expression, and (?!expr) is regex NOT operator.

Categories : Python

Using Python to Remove All Lines Matching Regex
You have to read the file try something like: import re inputfile = open('C:\temp\Scripts\remove.txt','w',encoding="utf8") inputfile.write(re.sub("[(.*?)yahoo(.*?) ","",inputfile.read())) file.close() outputfile.close()

Categories : Python

python: number range to regex matching string
[assuming you need this because it's some weird 3rd party system that requires regexp] New Approach the more i think about Frederik's comment, the more i agree. the regexp engine should be able to compile this down to a compact DFA, even if the input string is long. for many cases, the following is a sensible solution: import re def regexp(lo, hi): fmt = '%%0%dd' % len(str(hi)) return re.compile('(%s)' % '|'.join(fmt % i for i in range(lo, hi+1))) (it works fine with all the numerical ranges in the tests below, including 99519000 - 99519099. a rough back-of-the-envelope calculation suggests that 9 digit numbers are about the limit with 1GB of memory. that's if most numbers that size are matched; if only a few are matched you can go much larger.). Old Approach [updated a

Categories : Python

python regex not matching decoded unicode string
 There isn't a word boundary between the letter グ and the number 1—they are both alphanumerics. When Unicode-aware regex is being used this is correctly handled, hence no match. If you don't want to treat katakana and other non-ASCII letters as being alphanums, remove the re.UNICODE flag which controls this behaviour. When you send a byte string to a regex that was compiled from a Unicode string, it is automatically decoded. For some reason it seems to be decoded as ISO-8859-1 (rather than, say, sys.getdefaultencoding())... don't know why that is, but implicit encode/decode is in general evil to be avoided. The UTF-8 byte sequence for グ, when mis-decoded as ISO-8859-1, comes out as ã[control char]°. The degree sign is not an alphanum so you get a match.

Categories : Python

Python Regex: Matching a character on multi line?
>>> import re >>> print strs = """hello world <!-- %%$@_$^__#)^)&!_+]!*@&^}@[@%]()%+$&[(_@%+%$*^@$^!+]!&_#)_*}{}}!}_]$[%}@[{_@#_^{* @##&{#&{&)*%(]{{([*}@[@&]+!!*{)!}{%+{))])[!^})+)$]#{*+^((@^@}$[*a*$&^{$!@#$%)!@(&bc""" >>> re.findall(r'[a-zA-Z]+',strs.split('<!--')[-1]) ['a', 'bc']

Categories : Python

Hadoop Pig: Extract all substrings matching a given regular expression
If order does not matter, then this will work: -- foo is the tuple, and bar is the name of the chararray B = FOREACH A GENERATE TOKENIZE(foo.bar, ',') AS values: {T: (value: chararray)} ; C = FOREACH B { clean_values = FOREACH values GENERATE REGEX_EXTRACT(value, '(L[0-9]+)', 1) AS clean_value: chararray ; GENERATE clean_values ; } The schema and output are: C: {clean_values: {T: (clean_value: chararray)}} ({(L123),(L234),(L1),(L253764)}) ({(L23),(L2)}) ({(L5)}) Generally, if you don't know how many elements the array will have then a bag will be better.

Categories : Regex

Negative RegEx pattern matching in Python equivalent to Perl(!~ operator)
I don't understand your intent of doing a reverse regular expression. findall() seems a natural way of selecting your times, like this: ' '.join(re.findall(r'w{3,6}day:s*d{1,2}:d{1,2}[ap]ms*-s*d{1,2}:d{1,2}[ap]m', hr)) It yields: 'Monday: 11:30am - 9:30pm Tuesday: 11:30am - 9:30pm Wednesday: 11:30am - 10:00pm Thursday: 11:30am - 10:00pm Friday: 11:30am - 10:30pm Saturday: 11:00am - 10:30pm Sunday: 10:30am - 9:30pm'

Categories : Python

Substrings using regex grouping in C#
Try this: // string strTargetString = @"Acceptance :DT_Ext_0062-12_012ed2 [Describe]"; // string strTargetString = @"Acceptance : DT_Ext_0062-12_012 (ed.2) , Describe"; string strTargetString = @"Acceptance of : DT_Ext_0062-12_012 (ed.2) , Describe to me"; const string strRegex = @".*:s*(DT_Ext_d{4}-d{2}_d{3})s*W*(ed).?(d+)(W*[,])?(.*)"; RegexOptions myRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.CultureInvariant; Regex myRegex = new Regex(strRegex, myRegexOptions); foreach(Match myMatch in myRegex.Matches(strTargetString)) { if(myMatch.Success) { // Add your code here var value = new { Value1 = myMatch.Groups[1].Value, Value2 = myMatch.Groups[2].Value, Value3 = myMatch.Groups[3].Value,

Categories : C#

Search for multiple RegEx substrings
Don't search for the pound sign. Search for the numbers, then manually add the pound sign yourself. import re strings = [ "5.20", "$5.20", "$.50", "$5", "Shipping is free", "Shipping: not specified", "free", "not specified", ] pattern = r""" d* #A digit 0 or more times [.]? #A dot, optional d+ #A digit, one or more times | free #Or the word free | not s+ specified #Or the phrase "not specified" """ regex = re.compile(pattern, flags=re.X) results = [] for string in strings: md = re.search(regex, string) if md: match = md.group() if re.search(r"d", match): match = "$" + match results.append(match) else:

Categories : Python

Regex for identifying and seperate multiple substrings from a single string
You would need to do this with two seperate passes. Each pass would look for the values you're interested in Pass 1: (?<=s|^)(alan)(_)(d+) Pass 2: (?<=s|^)(roma)(_)(d+) However you wanted to know how to do this without the two pass solution. The best you could use an expression which places each match into a separate capture group, but I think this would be more cumbersome: (?<=s|^)(?=alan|roma)(alan)?(roma)?(_)(d+) Live Demo Sample Text How alan_613 are you roma_222 all doing alan_611 Matches In this example note how capture group 1 gets alan, or capture group 2 gets roma [0][0] = alan_613 [0][1] = alan [0][2] = [0][3] = _ [0][4] = 613 [1][0] = roma_222 [1][1] = [1][2] = roma [1][3] = _ [1][4] = 222 [2][0] = alan_611 [2][1] = alan [2][2] = [2][3] = _ [2][4]

Categories : Java

Break strings into substrings based on delimiters, with empty substrings
Now that we've cleared up what the question is about, here's the issue. Your gmatch pattern will return all of the matching substrings in the given string. However, your gmatch pattern uses "+". That means "one or more", which therefore cannot match an empty string. If it encounters a ^ character, it just skips it. But, if you just tried :gmatch("[^^]*"), which allows empty matches, the problem is that it would effectively turn every ^ character into an empty match. Which is not what you want. What you want is to eat the ^ at the end of a substring. But, if you try :gmatch("([^^])^"), you'll find that it won't return the last string. That's because the last string doesn't end with ^, so it isn't a valid match. The closest you can get with gmatch is this pattern: "([^^]*)^?". This has t

Categories : Lua

R matching more than 2 conditions and return the response value
merge(data, index, by.x=c("ind1", "ind2"), by.y=c("x1", "x2"), all.x=TRUE, all.y=FALSE) will give you the x and y values for each matching combination of ind1 and ind2, and x1 and x2. All combinations of x1 and x2 will be kept (even if that combination of ind1 and ind2 doesn't occur in index, but combinations of ind1 and ind2 that don't occur in data will be dropped. As written, the solution will keep x3 and y values, but if you'd like to drop the y values you can use merge(data[ ,-3], ... as per @Ferdinand.kraft 's suggestion.

Categories : R

how to eliminate the rows of a matrix matching certain conditions? in R
mm[rowSums(mm != 0) > 1,] # [,1] [,2] [,3] [,4] #[1,] 1 0 2 0 #[2,] 2 2 0 2 #[3,] 0 0 2 1 #[4,] 3 1 0 0 #[5,] 0 2 1 0 #[6,] 4 0 1 3

Categories : R

Ruby Regex: Matching a < without a matching >
This finds and converts <s that are not followed by a >: result = subject.gsub(/<(?![^<>]*>)/, '&lt;') Explanation: < # Match a < (?! # only if the following regex can't match here: [^<>]* # any number of characters except angle brackets > # followed by a closing angle bracket. ) # (End of negative lookahead assertion)

Categories : Ruby

Regular Expression - Matching and extracting complicated conditions
Regular expressions are not what you need. They are used to match a certain pattern, not a certain length. If you are holding the data in a string, myString.length <= 8000 is all you need for the character count (using the correct syntax for your language, of course). For the number of lines, you will have to count the number of sequences in your string (can be done iteratively). To get the first four lines, just find the 4th and get everything before that with a substring method.

Categories : Regex

Jenkins view to list jobs matching two conditions
You can use the View Job Filters plugin for this - it lets you build views which chain together filters to create the custom views, and the filter options include: Job name, including regular expression filters (i.e. you could filter by ^.*trunk.*$) Job status I think that should allow you to build the list view you need. The image below shows an example taken from the build system I manage, where we were able to create a view with the same characteristics as you require (naturally exchange labos for trunk).

Categories : Jenkins

Fetch Multiple records from the Table matching the conditions
You should do the subquery in the FROM part of your top level query and not in the SELECT. Try to do it like SELECT seminar, faq, dev_mode, meta_api, trading_signal FROM (subselect) as seminars_table, (subselect) as faq_table.... WHERE join_contition_1 and join_condition_1; The Subselects then need to contain the join columns.

Categories : Mysql

Python permutations including substrings
import itertools def all_permutations_substrings(a_str): return ( ''.join(item) for length in xrange(1, len(a_str)+1) for item in itertools.permutations(a_str, length)) Note, however, that this is true permutations - as in, hello will have any substring permutation that has two ls in it twice, since the l's will be considered "unique". If you wanted to get rid of that, you could pass it through a set(): all_permutations_no_dupes = set(all_permutations_substrings(a_str))

Categories : Python

What is the fastest way to filter a set of substrings from a string in python?
Use an re: import re shorterstring = re.sub('very, |long ', '', 'some very, very, very, long string') You'll need to make sure that the substrings to replace with nothing are in descending order of length so that longer matches are replaced first. Or, you could avoid the chained calls, and use: reduce(lambda a, b: a.replace(b, ''), ['very, ', 'long '], s)

Categories : Python

Python extracting substrings within brackets in a string
params = sent.split(id1)[1].split(")")[0].lstrip("(") print params That should do what you want. That being said, there are better ways to do this. You could store your items as key:value pairs using a dictionary for example.

Categories : Python

Python match all substrings and reversed strings
You can use regex: >>> import re >>> pat = 'CATA' >>> strs = 'TCATATGCAAATAGCTGCATACCGA' >>> [m.start() for m in re.finditer(pat, strs)] [1, 17]

Categories : Python

PHP Regex with conditions
Here is one solution - I use a loop and a if-condition to solve my problem: $output = ''; $subject = '[%Time%] %Logger%.%Level%: %Message% ${$%Context%$}$ $ $%Extra% '; $replace = array('Hello', 'World', ''); $pattern = '/(${1}([^$]+)${1})?%Context%(${1}([^$]+)${1})?/'; foreach ($replace as $r) { if ($r == '') { $output .= preg_replace($pattern, '', $subject) . '<br>'; } else { $output .= preg_replace($pattern, '$2' . $r . '$4', $subject) . '<br>'; } } Output: [%Time%] %Logger%.%Level%: %Message% {Hello} $ $%Extra% [%Time%] %Logger%.%Level%: %Message% {World} $ $%Extra% [%Time%] %Logger%.%Level%: %Message% $ $%Extra%

Categories : PHP

How to create regex with following conditions?
Following regex should work for you: ^$?(?=.)(?:d{1,3})?(?:,?d{3})*(?:.d+)?$ Live Demo: http://www.rubular.com/r/0qQBwTRMOl Just remember to make digits before dot optional match and that enforces use of positive lookahead to make sure there is at least one digit after optional $ sign. Here is a non-regex based solution to validate currency (USD): $fmt = new NumberFormatter( 'en-US', NumberFormatter::CURRENCY ); #$num = "1.234.567,89 $"; $num = "$.04"; var_dump($fmt->parseCurrency($num, $curr)); // returns float value: float(0.04) var_dump($curr); // returns string representing currency: string(3) "USD"

Categories : PHP

Remove duplicates with conditions using ASP.NET Regex
You can use this: Regex.Replace(input, "(<Important text />)+", "<Important text />"); This will replace any instance of <Important text /> repeated one or more times with a single instance of <Important text />. Or more simply: Regex.Replace(input, "(<Important text />)+", "$1"); For example: var input = "<Important text /><Important text />Other random words"; var output = Regex.Replace(input, "(<Important text />)+", "$1"); Console.WriteLine(output); // <Important text />Other random words If you'd like to handle multiple such patterns at once, you should use an alternation (|), specifying each word you'd like to handle, along with a back-reference (1) to find repetition: Regex.Replace(input, @"(<(?:Important text|Ot

Categories : C#

Multiple conditions regex JavaScript split
You can do it like this: var text = "deux-cent-quatre-vingt-trois"; console.log(text.split(/(?:^|-)(quatre-vingt(?:-dix|s$)?|soixante-dix|[^-]+)/)); The idea is to add a capturing group whose content is added to the split list. The capturing group contains at first particular cases and after the most general, described with [^-]+ (all that is not a -) Notice: since quatre-vingt is written with a s when it is not followed by a number, i added s$ as a possibility.

Categories : Javascript

C# Regex matching
The following regexp matches one or more <P> or </P> tags: (?:</?P>s*)+ So if you place that between the other tags you have, you can get rid of them, i.e. strSText = Regex.Replace(strSText, @"<UL>s*(?:</?P>s*)+<LI>", "<UL><LI>", RegexOptions.IgnoreCase); strSText = Regex.Replace(strSText, @"</LI>s*(?:</?P>s*)+<LI>", "</LI><LI>", RegexOptions.IgnoreCase); strSText = Regex.Replace(strSText, @"</LI>s*(?:</?P>s*)+</UL>", "</LI></UL>", RegexOptions.IgnoreCase);

Categories : C#

Matching regex to set
You are going about it the wrong way. You simply leave the set of characters to the regular expression: re.search('[AGCNU]{5}', line) matches any 5-character pattern built from those 5 characters; that matches the same 3125 different combinations you generated with your set line, but doesn't need to build all possible combinations up front. Otherwise, your regular expression attempt had no correlation to your patterns variable, the pattern r'.*?(?:patterns)' would match 0 or more arbitrary characters, followed by the literal text 'patterns'.

Categories : Python

Regex: Matching exactly one occurrence
You can use this: ^[^{}]*{[^}]*}[^{}]*$ Explanation: ^[^{}]* // Match 0 or more occurrences of character other than [{}] { // Match a `{` [^}]* // Match 0 or more occurrences of character other than } } // Match a `}` [^{}]*$ // Match 0 or more occurrences of character other than [{}] You need to take care of nested braces or unmatched braces too

Categories : Regex

Understanding why Regex not matching
You have d in there, which only matches digits, and you've got letters in your value. The expression for the key itself is probably going to be [0-9a-z] instead of d. You're missing the closing quote in your regex - you've got a percent sign in there where the quote should be.

Categories : C#



© Copyright 2017 w3hello.com Publishing Limited. All rights reserved.