Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
I am trying to match the following pattern
sn,n+1
where n is an integer. Examples include
s1,2 s3,4
Right now I know how to match the following: sn,n
(for example s1,1) with regex. The syntax I use for this is s(\d+),\1
Is it possible to do something like the following? s(\d+),\1+1
More detail on my specific problem (these details aren't necessarily relevant to the solution! But I am including them anyway), I am using CST, an electromagnetic simulator that supports regex for sorting s-parameter data. With a high port count, it is cumbersome to manually select isolation between ports. So I want to use a regex in the above way.
Regex can not do arithmetic but python does. You can find all matching patterns in your string and filter them later with python. This might help:
Regex
import re
string= 's1,2 test s2,3 , test test s12,12'
pattern = 's\d+,\d+'
pattern_list = re.findall(pattern, string)
print(pattern_list)
['s1,2', 's2,3', 's12,12']
Filtering with Python
result_list = [i for i in pattern_list if int(i.split(',')[0].split('s')[1]) + 1 == int(i.split(',')[1]) ]
print(result_list)
['s1,2', 's2,3']
Basically, it finds all patterns by using regex then python checks the patterns that fits to sn,n+1
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to parse some open-source code of Python to check if the source contains some specific patterns.
For example:
for i in range...:
if(i == 2):
.......
I might want to find if the source code contains a pattern just like above: an if statement inside a for loop. I know the expression pattern matching technique, but it does not work for this case.
Does anyone know how to find this kind of pattern matching automatically? Any useful tool?
Use ast.parse().
import ast
code = '''
for i in range(1, 10):
if (i == 2):
print(i)
'''
parsed = ast.parse(code)
for stmt in parsed.body:
if isinstance(stmt, ast.For):
for stmt2 in stmt.body:
if isinstance(stmt2, ast.If):
print("Found if in for")
break
This example is very simple, it only looks for for at the top level of the code, and if at the second level. You should be able to extend it to a recursive solution that searches for nested constructs.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
So I'm trying to get urls that contain specific strings, but also avoiding urls that contain a bad string.
So I don't want any urls that contain the string "/inventory/all/", and I only want urls that contain either the string "/inventories/" or "/inventory/2017/"
So I've managed to exclude at least the urls with strings that contain "/inventory/all/" by:
get_urls = soup.findAll('a', href=re.compile('^(?!.*/inventory/all/).*$'))
But when I try to include the strings I do want to get, then it no longer works, I tried:
get_urls = soup.findAll('a', href=re.compile('^(?!.*/inventory/all/).*$'|/inventories/|/inventory/2017/'))
Thanks for the help, I'm quite the novice
you can use the following regex:
^(?=.*inventor(?:ies|y/2017))^(?:(?!inventory/all).)+$
^(?=.*inventor(?:ies|y/2017)) This is a look ahead that ensures that we are just looking for strings with either inventories or inventory/2017. For fewer backtracking, you need to anchor it ie ^ which shows that the matching should start at the beginning of the sentence. Thus just doing ^.*inventor(?:ies|y/2017).*$ should be enough since the only ones selected are the two.
^(?:(?!inventory/all).)+$ this part is a negative look ahead which asserts that from the beginning of the string to the end of the string there is no inverntory/all. I added this part in case you find a string that is of the format inventoy/2017/inventory/all This will be dropped.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How do I match the following pattern using re?
2016-02-13 02:00:00.0,3525,http://www.heatherllindsey.com/2016/02/my-husband-left-his-9-5-job-for-good-it.html,158,0,2584490
I used python's split() function to separate the attributes out but as the data is huge, the process is getting killed due to memory errors.
If you put the long version of string it would be better.
So how can you make it ? That is the answer:
import re
str = "2016-02-13 02:00:00.0,3525,http://www.heatherllindsey.com/2016/02/my-husband-left-his-9-5-job-for-good-it.html,158,0,2584490"
pattern = re.compile("(.*?),", re.DOTALL) #we use re.DOTALL to continue splitting after endlines.
result = pattern.findall(str) #we can't find the last statement (2584490) because of the pattern so we will apply second process
pattern = re.compile("(.*?)", re.DOTALL)
str2 = str[-50:-1]+str[-1] #we take last partition of string to find out last statement by using split() method
result.append(str2.split(",")[-1])
print result
It works...
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a content like this:
aid: "1168577519", cmt_id = 1168594403;
Now I want to get all number sequence:
1168577519
1168594403
by regex.
I have never meet regex problem, but this time I should use it to do some parse job.
Now I can just get sequence after "aid" and "cmt_id" respectively. I don't know how to merge them into one regex.
My current progress:
pattern = re.compile('(?<=aid: ").*?(?=",)')
print pattern.findall(s)
and
pattern = re.compile('(?<=cmt_id = ).*?(?=;)')
print pattern.findall(s)
There are many different approaches to designing a suitable regular expression which depend on the range of possible inputs you are likely to encounter.
The following would solve your exact question but could fail given different styled input. You need to provide more details, but this would be a start.
re_content = re.search("aid\: \"([0-9]*?)\",\W*cmt_id = ([0-9]*?);", input)
print re_content.groups()
This gives the following output:
('1168577519', '1168594403')
This example assumes that there might be other numbers in your input, and you are trying to extract just the aid and cmt_id values.
The simplest solution is to use re.findall
Example
>>> import re
>>> string = 'aid: "1168577519", cmt_id = 1168594403;'
>>> re.findall(r'\d+', string)
['1168577519', '1168594403']
>>>
\d+ matches one or more digits.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
for some reason when I get regex to get the number i need it returns none.
But when I run it here http://regexr.com/38n3o it works
the regex was designed to get the last number of the ip so it can be removed
lanip=74.125.224.72
notorm=re.search("/([1-9])\w+$/g", lanip)
That is not how you define a regular expressions in Python. The correct way would be:
import re
lanip="74.125.224.72"
notorm=re.search("([1-9])\w+$", lanip)
print notorm
Output:
<_sre.SRE_Match object at 0x10131df30>
You were using a javascript regex style. To read more on correct python syntax read the documentation
If you want to match the last number of an IP use:
import re
lanip="74.125.224.72"
notorm=re.search("(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", lanip)
print notorm.group(4)
Output:
72
Regex used from http://www.regular-expressions.info/examples.html
Your example did work in this scenario, but would match a lot of false positives.
What is lanip's type? That can't run.
It needs to be a string, i.e.
lanip = "74.125.224.72"
Also your RE syntax looks strange, make sure you've read the documentation on Python's RE syntax.