Posts

Showing posts with the label regex

Regex - How to capture all iterations of a repeating pattern?

Image
Clash Royale CLAN TAG #URR8PPP Regex - How to capture all iterations of a repeating pattern? Take a look at this example, I want to capture not just the last iteration (which is 5), but also 2, 3 and 4. It says here: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations... but I don't know how to do this. Since I need this in C++, I was looking for a way to get all iterations of capturing group using some regex functions in C++, but I always end up with same groups which the website finds. How about instruction d((,d)*) ? (IMHO, this is meant in the cited text.) Though, this leaves still the task to separate the inner matches... – Scheff 17 mins ago instruction d((,d)*) Thou...

How to return positive and negative numbers when reading a file

Image
Clash Royale CLAN TAG #URR8PPP How to return positive and negative numbers when reading a file I am writing a code that reads a file line by line and return only the following lines, as an example: int int0 = (-953); int int1 = (-411); int int2 = 5471; int int3 = 823; After that I would like to return only the numbers both positive and negative. To do that, I wrote the following: String str = line.replaceAll("\D+",""); System.out.println(str); The result of running this code is: 0953 1411 25471 3823 The output that I seek is: -953 -411 5471 823 How can I do that? does the number can be float ? – YCF_L 1 hour ago @Thomas Look at the variable naming 0, 1, 2, 3, then look at the start of each output number ;) – Glains ...

How to escape from this regex with the output?

Image
Clash Royale CLAN TAG #URR8PPP How to escape from this regex with the output? I want the domain name to be separated for some other calculation. example.uk.db.com sed '/.[a-zA-Z].*' - this matches ".uk.db.com" when I give backslash it totally escapes without the return. What does "when I give backslash it totally escapes without the return" mean? Please show the whole code. – Wiktor Stribiżew 2 mins ago By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Replace character string between new lines

Image
Clash Royale CLAN TAG #URR8PPP Replace character string between new lines I am trying to edit a file such that strings flanked by new lines are removed. My file looks like: ENSG00000000460_chr1.dat varX data data data data data varX data data data data data varX data data data data data ENSG00000005801_chr11.dat ENSG00000006007_chr16.dat ENSG00000006607_chr2.dat varX data data data data data varX data data data data data ENSG00000010219_chr12.dat ENSG00000011052_chr17.dat The output I am trying to get would delete lines which are flanked by new lines (and delete the new lines) resulting in output which looks like: ENSG00000000460_chr1.dat varX data data data data data varX data data data data data varX data data data data data ENSG00000006607_chr2.dat varX data data data data data varX data data data data data I have tried various ideas in sed, but it either returns: sed 's/[na-zA-Z0-9n]//g' file.txt | head _. . .- . . . . . . .-...

RegularExpression: match and extract long domain

Image
Clash Royale CLAN TAG #URR8PPP RegularExpression: match and extract long domain I want to match and extract domain form strings,and I got an equation: result = re.findall(r"(^((?!-))(xn--)?[a-z0-9][a-z0-9-_]{0,61}[a-z0-9]{0,1}.(xn--)?([a-z0-9-]{1,61}|[a-z0-9-]{1,30}.[a-z]{2,})$)", text) It does well for domain like : example.org example.org.eu but it cannot work for domain like : sub_example.example.org.eu so,I am asking for helping improving the equation. By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Regex - replace space in string that has '.com Capital letter' with new line

Image
Clash Royale CLAN TAG #URR8PPP Regex - replace space in string that has '.com Capital letter' with new line I have a string with a space that I need to replace. The pattern is .com followed by a space and then any capital letter. An example would be: ".com T" The space between the .com and T needs to be replaced by a new line. Have you made any attempt to write such a regular expression yourself yet? Post what you've tried – CertainPerformance 12 mins ago Construct your regex here: regex101.com - make sure to switch to pythons syntax. After finishing it use re.sub from the re - module and code the python prog to do it. – Patrick Artner 9 mins ago ...

How to match match the string from the list using python regular expression?

Image
Clash Royale CLAN TAG #URR8PPP How to match match the string from the list using python regular expression? Hi I have a list of strings ['gool', 'gol', 'log'] I need to create a regex such a way that only gol and log has to print. If i using re.search all the strings are getting printed and re.match will check the starting of the letter so only gol and gool are getting printed. We can give by giving condition of len = 3 By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Counting all the numbers from a text file with re?

Image
Clash Royale CLAN TAG #URR8PPP Counting all the numbers from a text file with re? I am with the basics of RE, I need open a file with a lot of lines, take only the numbers and shows the average. This is my poor try: import re try: ufile = open(input('What file are you using? ')) except: print('File don't founded.') exit() lnumbers = for line in ufile: numbers = re.findall('([0-9.]+) ', line) if len(numbers) > 0: lnumbers.append(numbers) I don't know what to do next. 1 Answer 1 Assuming that a given number would never wrap around from one line to the next, then a slight modification of your code should work: for line in ufile: numbers = re.findall('[0-9]+(.[0-9]+)?', line) for number in numbers: lnumbers.append(numbers) if len(lnumbers) > 0: print sum(lnumbers) / len(lnumbers) else: print "no number...

C++: Regex pattern

Image
Clash Royale CLAN TAG #URR8PPP C++: Regex pattern I got a regex pattern: (~[A-Z]){10,30} (Thanks to KekuSemau). And I need to edit it, so it will skip 1 letter. So it will be like down below. Input: CABBYCRDCEBFYGGHQIPJOK Output: A B C D E F G H I J K Perhaps for that input you could match the first character and capture the second character in a group and replace with a whitespace and the capturing group [A-Z]([A-Z])? – The fourth bird 17 hours ago [A-Z]([A-Z])? What's with the ~ ? Just replace .(.) with ` $1` (space+$1). See it here at regex101. – ClasG 15 hours ago ~ .(.) 2 Answers ...

PHP preg_replace error when using array

Image
Clash Royale CLAN TAG #URR8PPP PHP preg_replace error when using array We have got web app which does replacing some text with another using str_replace(). Find strings and replace strings are stored in template file. We what to replace str_replace() function to preg_replace() to have possibility to use regex in find strings (to set them in the same template file). In original scripts we have such parts of php code. In one file: class SiteConfig { // Strings to search for in HTML before processing begins (used with $replace_string) public $find_string = array(); // Strings to replace those found in $find_string before HTML processing begins public $replace_string = array(); // a lot of code goes here public function append(SiteConfig $newconfig) { foreach (array('find_string', 'replace_string') as $var) { // append array elements for this config variable from $newconfig to this config //$this->$var = $this->$var + $newconfig->$var; $...

regex template engine delete carriage return

Image
Clash Royale CLAN TAG #URR8PPP regex template engine delete carriage return I'm using this template engine: https://github.com/krasimir/absurd/blob/master/lib/processors/html/helpers/TemplateEngine.js from blog: http://krasimirtsonev.com/blog/article/Javascript-template-engine-in-just-20-line const templateEngine = (html, options = {}) => { let re = /<%(.+?)%>/g, reExp = /(^( )?(var|if|for|else|switch|case|break|{|}|;))(.*)?/g, code = 'with(obj) { var r=;n', cursor = 0, result, match; const add = function(line, js) { js? (code += line.match(reExp) ? line + 'n' : 'r.push(' + line + ');n') : (code += line != '' ? 'r.push("' + line.replace(/"/g, '\"') + '");n' : ''); return add; }; while(match = re.exec(html)) { add(html.slice(cursor, match.index))(match[1], true); cursor = match.index + m...

Regular Expression_How to extract several matching patterns from a line?

Image
Clash Royale CLAN TAG #URR8PPP Regular Expression_How to extract several matching patterns from a line? I have a .csv document consists of several lines. In each line I have tab separated informations such as, name_1:ayse t name_2:fatma t birth_date_1:24 t birth_date_2:august t birth_date_3:2018 t death_date:2100 t location:turkey. The sequence of these informations may not be same in each line and there many informations like this in each line. What am I trying to do is to get a specific part of the string which only has "birth_date" information in it. I am managed to get only all 3 strings related with birth date as follows ['birth_date_1', 'birth_date_2', 'birth_date_3'] with the help of below code. inputfile = open('ornek_data.csv','r',encoding="utf-8") for rownum, line in enumerate(inputfile): pattern_birth = re.compile(r"w*birth_datew*",re.IGNORECASE) if pattern_birth.search(line) is not None: ...

How many time the string matches

Image
Clash Royale CLAN TAG #URR8PPP How many time the string matches I have a pattern like this one here *b* and a string like this one "abcdb" witch is matches twice. *b* I want to calculate the number of time string matches, but I have no clue how to achieve this in C language since I'm just a beginner. Is there any hints of how can make this function in c? In plain simple standard C? You can't, since C doesn't have any regular expression functions. There are other ways to do it than regular expressions though. I suggest you read a little about the strstr function. – Some programmer dude 8 mins ago strstr Yes, I know but I need way out with the asterisk, sorry that It didn't show up earlier, I 've modified my question. ...

Using regex to split list of strings into list of lists based on starting character

Image
Clash Royale CLAN TAG #URR8PPP Using regex to split list of strings into list of lists based on starting character I have a very long list of 9000+ elements. For example Sample is below: lst = ['0sbsd uu', '7fsfss us', 'Bsfsd ll', 'Zufss dl', 'fasfs ff', '8fsdr2 us', 'It fss', 'Fsffsfds f'] I have to split lst into a list of 15 "almost" equal size sublists such that each sublist contains all the entries starting with a given character range (say 0 - c, d - h, .... ). lst 0 - c, d - h, .... By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Regex for finding between 1 and 3 character in a string

Image
Clash Royale CLAN TAG #URR8PPP Regex for finding between 1 and 3 character in a string I am trying to write a regex which should return true, if [A-Za-z] is occured between 1 and 3, but I am not able to do this public static void main(String args) { String regex = "(?:([A-Za-z]*){3}).*"; String regex1 = "(?=((([A-Za-z]){1}){1,3})).*"; Pattern pattern = Pattern.compile(regex); System.out.println(pattern.matcher("AD1CDD").find()); } Note: for consecutive 3 characters I am able to write it, but what I want to achieve is the occurrence should be between 1 and 3 only for the entire string. If there are 4 characters, it should return false. I have used look-ahead to achieve this Could you add some inputs and outputs for better understanding. – JavaFan 3 mins ago Try ^[^A-Za...

Regex to match 2 digit but different numbers

Image
Clash Royale CLAN TAG #URR8PPP Regex to match 2 digit but different numbers I'm working regex in recent days and now need to make regex which is match with 2 digit but the digits should be different each other For example followings will be matched: 56, 78, 20 ... But followings should not be matched: 22, 33, 66 or 99 Already wasted few days for this solution. So any suggestion will be welcome. 1 Answer 1 Capture the first digit, then use negative lookahead with a backreference to that first digit to ensure it isn't repeated: (d)(?!1)d https://regex101.com/r/AxH6s8/1 If you need a named group instead: (?<first>d)(?!k<first>)d For a general solution of n digits in a row without any repeated digits, you can do something similar, except put d* inside the negative lookahead, before the backreference: n d* ^(?:(d)(?!d*g{-1}))+$ https://regex101.com/r/AxH6s8/2 ...

How to give range in lookhead using regex e.g ^(?=(.*[a-z]){1,3})(?=.*[0-9]).{2,5}$

Image
Clash Royale CLAN TAG #URR8PPP How to give range in lookhead using regex e.g ^(?=(.*[a-z]){1,3})(?=.*[0-9]).{2,5}$ Question- 1]String length is 2 to 5 2]String contains at least 1 char and maximum 3 char 3]Atleast one number I want do using lookhead. What i tried but not working ^(?=(. [a-z]){1,3})(?=. [0-9]).{2,5}$ ^(?=(.*[a-z]){1,3})(?=.*[0-9]).{2,5}$ – user3890872 16 mins ago Sorry, that was me being practically blind... ;-) – Yunnosch 13 mins ago Might you have an example input set? – CertainPerformance 5 mins ago ...