w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories
How to match several lines with regex
You're repeating the capturing group itself, which overwrites each match with the next repetition. If you do this exp = ur'a b c d e {2}((?:.* ){5})' you get the five lines together. You can't get to the individual matches unless you spell out the groups manually: exp = ur'a b c d e {2}(.* )(.* )(.* )(.* )(.* )'

Categories : Python

perl REGEX to match mutiple lines
You should never use `cat errors.log` in Perl. Reading files is something that is simple and efficient in Perl, so learn it instead. perl -lnwe 'BEGIN { $/= $/x3 } if (/receive status is 2/) { print /requests*=s*(.+)/ }' errors.log Explanation: -n will treat the arguments to the script as file name arguments and open those files for reading. -l will handle line endings (for print here) The code inside BEGIN sets the input record separator to three newlines (or whatever your OS is using), which allows us to read the input file in chunks, reading up until three consecutive newlines. And the rest is just two simple regexes. Because we spent some time making sure to keep the chunks separate, we can now use simpler regexes. Output: /Lookup?url=URL1 /Lookup?url=URL3

Categories : Regex

Regex with possible empty matches and multi-line match
I think I'd avoid using regex to do this task, instead split it into sub-tasks. Basic algorithm outline Split the string on using explode Loop over the resulting array Split the resulting strings on : also using explode with a limit of 2. If the produced array's length is less than 2, add the entirety of the data to the previous key's value Else, use the first array index as your key, the second as the value unless the split colon was escaped (in which case, instead add the key + split + value to the previous key's value) This algorithm does assume there are no keys with escaped colons. Escaped colons in values will be dealt with just fine (i.e. user input). Code $str = <<<EOT FooID: 123456 Name: Chuck When: 01/02/2013 01:23:45 InternalID: User Message: Hello, this is

Categories : PHP

Find all lines that match regex pattern and grab part of string
You can do something like this(no need of regex): Use str.startswith to check if a line starts with '\': >>> strs = "\BTLCMOODY01 MRA Server " >>> strs.startswith('\') True Then use a combination of str.split and str.lstrip to get the first word: >>> strs.split(None, 1) ['\BTLCMOODY01', 'MRA Server '] #apply str.lstrip on the first item >>> strs.split(None, 1)[0].lstrip('\') 'BTLCMOODY01' Code: >>> with open('abc1') as f: ... for line in f: ... if line.startswith('\'): #check if the line startswith `` ... print line.split(None,1)[0].lstrip('\') ... TESTHOSTDEV01 TESTHOSTDEVDB01 TESTHOSTDEVDBQA TESTHOSTDEVQA02 BTLCMOODY01 BTLCSTG05 BTLCWEB02 BTLCWSUS01 HIMSAPP01 SLVAPP01 TORAAPP01 HNSVA

Categories : Python

How can I create a regex that parses all lines of a string for a substring, and returns a match only if the substring is not found?
The problem is that the dot doesn't match newlines. As Jerry suggests it, you can use the singleline mode (or dotall mode) to allow the dot to match newlines. An other way consists to avoid the dot, example for the word "test": ^(?>[^t]+|Bt|t(?!est))*$ Note that this way is more performant since the lookahead is tested only when there is a "t" preceded by a word boundary. ( vs on each character with ^((?!test).)*$)

Categories : C#

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

Regex in python that looks into pattern over multiple lines
Here I have added regular expressions to a list and tried finding a match one after the other... Is this what you were looking for ?? import re f = open("C:\Users\mridulp\Desktop\temp\file1.txt") regexpList = [re.compile("(([A-Z]+s+)+,[A-Z]+)"), re.compile("^.*(d{7,}s+d{7,}s+(d{2}/d{2}/d{2}s+){2})"), re.compile("^.*(d{4}s+d{2}/d{2}/d{2}).*")] lines = f.readlines() i = 0 for l in lines: mObj = regexpList[i].match(l) if mObj: print mObj.group(1) i = i + 1 if i > 2: i = 0 f.close()

Categories : Python

Python Regex: find all lines that start with '{' and end with '}'
You need re.findall() (or re.finditer) >>> import re >>> for r in re.findall(r'{.*}', data)[:18]: print r {"pitch":-30.778193,"yaw":-124.63285,"roll":-8.977466} {"pitch":-30.856342,"yaw":-124.57556,"roll":-7.7220345} {"pitch":-31.574106,"yaw":-124.65623,"roll":-7.911794} {"pitch":-30.479567,"yaw":-124.24301,"roll":-8.730827} {"pitch":-29.30239,"yaw":-123.97949,"roll":-8.134723} {"pitch":-29.84712,"yaw":-124.584465,"roll":-8.588374} {"pitch":-31.072054,"yaw":-124.707466,"roll":-8.877062} {"pitch":-31.493435,"yaw":-124.75457,"roll":-9.019922} {"pitch":-29.591925,"yaw":-124.960815,"roll":-9.379437} {"pitch":-29.37105,"yaw":-125.14427,"roll":-9.642341} {"pitch":-29.483717,"yaw":-125.16528,"roll":-9.687177} {"pitch":-30.903332,"yaw":-124.603935,"roll":-9.423098} {"pitch

Categories : Python

Python 3 Regex Last Match
Description This regex will match the string value XX which can be replaced with the user input. The regex will also require that the XX string be surrounded by white space or at the beginning of your sample text which prevents the accidental edge case where XX is found inside a word like EXXON. (?<=s|^)(xx)s.*?s(d+)(?=s|$) Code Example: I don't know python well enough to offer a proper python example, so I'm including a PHP example to simply show how the regex would work and the captured groups <?php $sourcestring="EXXON abcd XX blah blah 123 more blah blah"; preg_match('/(?<=s|^)(xx)s.*?s(d+)(?=s|$)/im',$sourcestring,$matches); echo "<pre>".print_r($matches,true); ?> $matches Array: ( [0] => XX blah blah 123 [1] => XX [2] => 123 )

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

Alternative way to extract lines from text (python-regex)
Firstly, I'm pretty sure that you mean data = open('database.txt').readlines() rather than read(). Otherwise, data will be a string rather than a list of lines, and your for line in data won't make any sense. At this point, you're really looking for an indexing solution by keyword, and naive search will stop being efficient enough to give you a timely result. There's really not another approach that's significantly more efficient or less complex. You're going to have to grind your teeth and accept the cost of looking through the entire database. Also, your database can't be all that big if it completely fits in memory :) That said, there are other ways you could do it that will probably be more efficient: Put your keywords in a set, then tokenise the input data into word and look a

Categories : Python

Python regex to match IP range
You can use this: re.compile(r"66.249.(?:6[4-9]|[78]d|9[0-5]).d{1,3}$") if you are motivated you can replace d{1,3} by : (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) Explanation: A regex engine doesn't know what a numeric range is. The only way to describe a range is to write all the possibilities with alternations: 6[4-9] | [78][0-9] | 9[0-5] 6 can be followed by 4 to 9 --> 64 to 69 7 or 8 can be followed by 0 to 9 --> 70 to 89 9 can be followed by 0 to 5 --> 90 to 95

Categories : Python

Creating a match with regex in Python
If you always want to gather the price, you can do this: regex_string = r'= (.*? per contract)' This catches: '625 dollars per contract' All it does is take anything between the equal sign (and space after it) and the words "per contract." Since your regex assumes that the words "per contract" will always be present, so does mine. If you just want the price, you can do this instead: >>>regex_string = r'= (.*?) per contract' >>>matchobj = re.search(regex_string, "(.0025 = 625 dollars per contract)") >>>matchobj.groups() ('625 dollars',)

Categories : Python

How to write a regex in python to match this?
You need to add the python regex for whitespace into your pattern to account for the newlines. Try this: regex = r"[1-9]+) .*s.*" s is the regex for any whitespace

Categories : Python

Python regex to match 2 distinct delimiters
If I understand your needs correctly, you could use this: [[(?:(?<uid>w+)::)?(?!.*::)(?<page>[^| fv]+)(?:|(?<alias>[^| fv]+))?]] ^^^^^^^^ See here for a demo. I added a negative lookahead after the uid capture. I have given names to the captured groups but if you don't want them, that's the one without named captured groups: [[(?:(w+)::)?(?!.*::)([^| fv]+)(?:|([^| fv]+))?]]

Categories : Python

Python regex re.match() not returning any results
Regular expressions are used when you want a "fuzzy" match - that is, you aren't sure if the string you are looking for will be identical every time. In this case, the string you are looking for appears to be exactly -----BEGIN PGP MESSAGE----. In this case, the string.find function will be simpler to use and faster to boot. >>> a = "This is a PGP encrypted email. -----BEGIN PGP MESSAGE----- !@#$%^..." >>> b = "This is not encrypted. My hovercraft is full of eels." #example strings >>> a.find("-----BEGIN PGP MESSAGE-----") 30 # Return value '30' means that the search string was found at index 30 of source string >>> b.find("-----BEGIN PGP MESSAGE-----") -1 # -1 means 'not found in the source string' >>>

Categories : Python

how do i return a string from a regex match in python
You should use re.MatchObject.group(0). Like imtag = re.match(r'<img.*?>', line).group(0) Edit: You also might be better off doing something like imgtag = re.match(r'<img.*?>',line) if imtag: print("yo it's a {}".format(imgtag.group(0))) to eliminate all the Nones.

Categories : Python

Match the folder list by using regex in python
IDd+_d+_d+ Matches the ID followed by three groups of one or more digits, separated by underscore. And the Python code: > import re > str = "https://10.0.4.3/myrepos/Projects/ID87_070_138" > print re.findall(r"IDd+_d+_d+", str) With the result: ['ID87_070_138']

Categories : Python

Python regex to match multiple times
You've got extra /'s in the regex. In python the pattern should just be a string. e.g. instead of this: pattern = re.compile('/review: (http://url.com/(d+)s?)+/', re.IGNORECASE) It should be: pattern = re.compile('review: (http://url.com/(d+)s?)+', re.IGNORECASE) Also typically in python you'd actually use a "raw" string like this: pattern = re.compile(r'review: (http://url.com/(d+)s?)+', re.IGNORECASE) The extra r on the front of the string saves you from having to do lots of backslash escaping etc.

Categories : Python

Python regex to match a specific word
You should use re.search here not re.match. From the docs on re.match: If you want to locate a match anywhere in string, use search() instead. If you're looking for the exact word 'Not Ok' then use  word boundaries, otherwise if you're only looking for a substring 'Not Ok' then use simple : if 'Not Ok' in string. >>> strs = 'Test result 1: Not Ok -31.08' >>> re.search(r'Not Ok',strs).group(0) 'Not Ok' >>> match = re.search(r'Not Ok',strs) >>> if match: ... print "Found" ... else: ... print "Not Found" ... Found

Categories : Python

Match to string length by using regex in python
Although it is not clear which 1 or 2 length character strings you want to accept I propose the following regex: regex = re.compile("^[a-zA-Z][a-zA-Z0-9.-]{0,28}[a-zA-Z0-9]$") As the middle set includes all other this will directly match all words with length 3-30 as you wish. I hope this regex also matches your 2 length strings (I just assumed that the first character must be a letter), you need to add something (using '|') for single letter matches.

Categories : Python

Python regex to match character a number of times
try with this kind of pattern (for 10 between 0 or 2 times): ^(([^1]+|1+(?=0))*10){0,2}([^1]+|1+(?=0))*$ You can easily adapt it for rabbit between 0 and 3 times: ^(([^r]+|r+(?=abbit))*rabbit){0,3}([^r]+|r+(?=abbit))*$

Categories : Python

Python regex: how to match strings that DO NOT contain an *exact* sentence?
Use a negative lookahead assertion like this: re.findall("(?!^.*This is message 12345. Ignore..*$).*", data) and also enable the m modifier, so that ^ and $ match the start and the end of a row.

Categories : Python

regex match all words in a python list not preeced by @
You could simply use a negative look-behind. A pattern like this should work: (?<!@)w+ Note the  (word boundaries) are there to ensure that it matches the whole word—without them it would match pples and uava.

Categories : Python

Python Regex Match for every single character in a "alphanumeric word"
You can use this regex that follows your description list: (?i)[dptu][a-z0-9]{3}[a-z][0-9]{5}[a-z]{4} (?i) is a modifier to make the pattern case insensitive

Categories : Regex

Python Regex: Question mark (?) doesn't match in middle of string
Regex d? simply means that it should optionally (?) match single digit (d). If you use something like this, it will work as you expect (match single digit anywhere in the string): d

Categories : Python

Ignore lines in a file till match and process lines after that
I'd use flip-flop operator: while(<DATA>) { next if 1 .. /Important/; print $_; } __DATA__ skip skip Important Lines below this Line keep keep output: keep keep

Categories : Perl

How can I get results that don't fully match using ElasticSearch?
You could use EdgeNGram tokenizer: http://www.elasticsearch.org/guide/reference/index-modules/analysis/edgengram-tokenizer/ Specify an index time analyzer using this, "analysis": { "filter": { "fulltext_ngrams": { "side": "front", "max_gram": 15, "min_gram": 3, "type": "edgeNGram" } }, "analyzer": { "fulltext_index": { "type": "custom", "filter": [ "standard", "lowercase", "asciifolding", "fulltext_ngrams" ], "type": "custom", "tokenizer": "standard" } } Then either specify as default index analyzer, or for a specific field mapping. When indexing a field with value jewelr

Categories : Elasticsearch

Check if part of lines match with lines in dataset
Lets call the two files Dirty and Clean. You could have a loop that indexes through Dirty and then another loop within that loop that indexes through clean to see if the line you are searching for exists. If the line does exist in Clean write it out into a new file called NEWDATABASE if not write what already exists in Dirty. If you are more specific with your question and provide what the lines look like, I could help more.

Categories : Python

Python Pass multiple variables through command to change multiple lines in a text file with regex
I replaced the for loop with the following code and it worked: with open('test1.txt') as f: line1 = f.readline() line2 = f.readline() line3 = f.readline() build = re.sub ('build='r'.*','build='+buildpath,line1) apps = re.sub ('apps='r'.*','apps='+appspath,line2) logs = re.sub ('logs='r'.*','logs='+logspath,line3) tf.write(build + apps + logs)

Categories : Python

Regex to match paths that don't match a specific pattern: Express Router
The following regex will match any path except those starting with /foo/ app.get(/^/([^f][^o][^o]|.{1,2}|.{4,})/.*$/, routes.index); I assume that this is a standard javascript regex.

Categories : Regex

How to match lines in one file against lines in another?
Try: if ($lineA =~ m/$lineU/sxm) Also, consider the possibility that you may have differing line endings in the file, and strip line endings prior to performing your comparison. Finally, I'm hoping that you recognize that you're ignoring the first line of each file by pulling a line prior to starting your while loops. my $lineU = <$unused>; my $lineA = <$all>; If you didn't want to do this, it would be better to initialize as such: my $lineU = q{}; my $lineA = q{};

Categories : Perl

Regex to match single new line. Regex to match double new line
To match exactly N repetitions of the same character you need lookaheads and lookbehinds (see Match exactly N repetitions of the same character). Since javascript doesn't support the latter, a pure regexp solution seems to be impossible. You'll have to use a helper function, for example: > x = "...a...aa...aaa...aaaa...a...aa" "...a...aa...aaa...aaaa...a...aa" > x.replace(/a+/g, function($0) { return $0.length == 2 ? '@@' : $0; }) "...a...@@...aaa...aaaa...a...@@"

Categories : Javascript

Regex to match only till first occurence of class match
You were missing ? Your regex would be (?i)(.*?)case[^a-zd]*(d+)(.*) You can toggle case insensitive match using (?i) in regex

Categories : Regex

Regex that match if the match contains special word
You're kind of on the right track with lookahead assertions: {{START}}(?:(?!{{END}})[sS])*specialword(?:(?!{{END}})[sS])*{{END}} Explanation: {{START}} # Match {{START}} (?: # Match... (?!{{END}}) # ...as long as we haven't reached {{END}} yet: [sS] # any character )* # any number of times. specialword # Match "specialword" (?: # Match (as before)... (?!{{END}}) # whatever follows, unless it's {{END}} [sS] )* {{END}} # Then finally match {{END}}

Categories : Regex

Regex - Find the match that is inside a match
You can try this regex: /href=[^>]+.pdf/ regex101 demo Most of the time, when you can avoid .* or .+ (or their lazy versions), it's better :) Also, don't forget to escape periods.

Categories : PHP

Java regex: need one regex to match all the formats specified
Try using a reluctant quantifier: _year:.*?s. .replaceAll("_year:.*?\s", "_year:Y ") System.out .println("utc-hour_of_year:2013-07-30T17 dsfsdgfsgf utc-week_of_year:2013-W31 dsfsdgfsdgf" .replaceAll("_year:.*?\s", "_year:Y ")); utc-hour_of_year:Y dsfsdgfsgf utc-week_of_year:Y dsfsdgfsdgf

Categories : Java

regex not returning match but there is clearly a match
You need to escape the dollar sign. start = '>$' end = '</td>' AnnualDiv = re.search('%s(.*)%s' % (start, end), s).group(1) The reason is that the $ is a special character in regex. (It matches the end of a string or before the newline.) This will set AnnualDiv to the string '0.48'. If you want to add the $, you can do it using this: AnnualDiv = "$%s" % re.search('%s(.*)%s' % (start, end), s).group(1)

Categories : Python

Regex.Match() won't match a substring
Try removing ^ and $: Regex regex = new Regex(@"[ABCEGHJKLMNPRSTVXY]{1}d{1}[A-Z]{1} *d{1}[A-Z]{1}d{1}", RegexOptions.None); ^ : The match must start at the beginning of the string or line. $ : The match must occur at the end of the string or before at the end of the line or string. If you want to match only in word boundaries you can use  as suggested by Mike Strobel: Regex regex = new Regex(@"[ABCEGHJKLMNPRSTVXY]{1}d{1}[A-Z]{1} *d{1}[A-Z]{1}d{1}", RegexOptions.None);

Categories : C#

Javascript regex to match a regex
A regular expression to match a regular expression is //((?![*+?])(?:[^ [/\]|\.|[(?:[^ ]\]|\.)*])+)/((?:g(?:im?|mi?)?|i(?:gm?|mg?)?|m(?:gi?|ig?)?)?)/ To break it down, / matches a literal / (?![*+?]) is necessary because /* starts a comment, not a regular expression. [^ [/\] matches any non-escape sequence character and non-start of character group [...] matches a character group which can contain an un-escaped /. \. matches a prefix of an escape sequence + is necessary because // is a line comment, not a regular expression. (?:g...)? matches any combination of non-repeating regular expression flags. So ugly. This doesn't attempt to pair parentheses, or check that repetition modifiers are not applied to themselves, but filters out most of the other ways that regular expressions

Categories : Javascript



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