How could I just match the first anchor tag and not all of them until the last one? Basically all of this: "<a...>...</a>" without the other ones? Would I need to sub the string before matching?
Here's what I got:
https://regex101.com/r/hXh2JI/1
Thank you!
Try
<a[^>]*>[^<]*</a>
I think this regex does what you're asking for. Add the global and multi line regex flags to capture all cases of <a> ... </a>.
Regex looks like this:
(<a>.*<\/a>)
Related
I am trying to formnulate a regex to get the ids from the below two strings examples:
/drugs/2/drug-19904-5106/magnesium-oxide-tablet/details
/drugs/2/drug-19906/magnesium-moxide-tablet/details
In the first case, I should get 19904-5106 and in the second case 19906.
So far I tried several, the closes I could get is [drugs/2/drug]-.*\d but would return g-19904-5106 and g-19907.
Please any help to get ride of the "g-"?
Thank you in advance.
When writing a regex expression, consider the patterns you see so that you can align it correctly. For example, if you know that your desired IDs always appear in something resembling ABCD-1234-5678 where 1234-5678 is the ID you want, then you can use that. If you also know that your IDs are always digits, then you can refine the search even more
For your example, using a regex string like
.+?-(\d+(?:-\d+)*)
should do the trick. In a python script that would look something like the following:
match = re.search(r'.+?-(\d+(?:-\d+)*)', my_string)
if match:
my_id = match.group(1)
The pattern may vary depending on the depth and complexity of your examples, but that works for both of the ones you provided
This is the closest I could find: \d+|.\d+-.\d+
I am trying to match email footers like
Thanks,
Regards
Thanks & Regards,
with the help of regular expression I am able to get first 2 cases,
({language_keyword_footer[0]}|{language_keyword_footer[1]}(.*?)(\S+(.*?)))
language keyword footer is the metadata that I have created to add more cases in future,
keywords = {
"en": {'header':["From:", "Subject:"],'footer':["Regards,", "Thanks,","Thanks & Regards,"]}}
The problem is when I use this approach it captures only Regards, and discards Thanks & Regards,
is there a way I can add it to the existing re and capture this space separated scenario as well, any help is appreciated
You need to sort the items by length in dedcending order as alternation patterns in an NFA regex engine follow "first matched, first served" principle (see the "Remember That The Regex Engine Is Eager" article). Also, do not forget to escape the strings used as literal pattern inside the regex.
You can use something like
pattern = r'({})(.*?)(\S+)'.format("|".join(map(re.escape, sorted(language_keyword_footer, key=len, reverse=True))))
The resulting pattern would look like (Thanks\ \&\ Regards,|Regards,|Thanks,)(.*?)(\S+) here, and will match Thanks & Regards,, Regards, or Thanks, (in Group 1), then any zero or more chars other than line break chars as few as possible (in Group 2), and then one or more non-whitespace chars (with (\S+)).
Note the .*? at the end of a regex pattern has no effect on the final result.
I am working on a wordle bot and I am trying to match words using regex. I am stuck at a problem where I need to look for specific permutations of a given word.
For example, if the word is "steal" these are all the permutations:
'tesla', 'stale', 'steal', 'taels', 'leats', 'setal', 'tales', 'slate', 'teals', 'stela', 'least', 'salet'.
I had some trouble creating a regex for this, but eventually stumbled on positive lookaheads which solved the issue. regex -
'(?=.*[s])(?=.*[l])(?=.*[a])(?=.*[t])(?=.*[e])'
But, if we are looking for specific permutations, how do we go about it?
For example words that look like 's[lt]a[lt]e'. The matching words are 'steal', 'stale', 'state'. But I want to limit the count of l and t in the matched word, which means the output should be 'steal' & 'stale'. 1 obvious solution is this regex r'slate|stale', but this is not a general solution. I am trying to arrive at a general solution for any scenario and the use of positive lookahead above seemed like a starting point. But I am unable to arrive at a solution.
Do we combine positive lookaheads with normal regex?
s(?=.*[lt])a(?=.*[lt])e (Did not work)
Or do we write nested lookaheads or something?
A few more regex that did not work -
s(?=.*[lt]a[tl]e)
s(?=.*[lt])(?=.*[a])(?=.*[lt])(?=.*[e])
I tried to look through the available posts on SO, but could not find anything that would help me understand this. Any help is appreciated.
You could append the regex which matches the permutations of interest to your existing regex. In your sample case, you would use:
(?=.*s)(?=.*l)(?=.*a)(?=.*t)(?=.*e)s[lt]a[lt]e
This will match only stale and slate; it won't match state because it fails the lookahead that requires an l in the word.
Note that you don't need the (?=.*s)(?=.*a)(?=.*e) in the above regex as they are required by the part that matches the permutations of interest. I've left them in to keep that part of the regex generic and not dependent on what follows it.
Demo on regex101
Note that to allow for duplicated characters you might want to change your lookaheads to something in this form:
(?=(?:[^s]*s){1}[^s]*)
You would change the quantifier on the group to match the number of occurrences of that character which are required.
There is one condition where I have to split my string in the manner that all the alphabetic characters should stay as one unit and everything else should be separated like the example shown below.
Example:
Some_var='12/1/20 Balance Brought Forward 150,585.80'
output_var=['12/1/20','Balance Brought Forward','150,585.80']
Yes, you could use some regex to get over this.
Some_var = '12/1/20 Balance Brought Forward 150,585.80'
match = re.split(r"([0-9\s\\\/\.,-]+|[a-zA-Z\s\\\/\.,-]+)", Some_var)
print(match)
You will get some extra spaces but you can trim that and you are good to go.
split isn't gonna cut it. You might wanna look into Regular Expressions (abbreviated regex) to accomplish this.
Here's a link to the Python docs: re module
As for a pattern, you could try using something like this:
([0-9\s\\\/\.,-]+|[a-zA-Z\s\\\/\.,-]+)
then trim each part of the output.
I need to rewrite a url like test.php?type=$1&val=$2
type will always be a string where as val could be a number or a string. I came up with the following
test/([a-zA-Z]+)/([a-zA-Z0-9|]+)/([0-9]+).html but as it was expected it will not work with something lke test/hello-world/23.html How can i included dashes in the expression?
You can escape them, so if you had
test/([a-zA-Z]+)/([a-zA-Z0-9\-|]+)/([0-9]+).html
This seems more like a question about regular expressions to me. test/([a-zA-Z-]+)/([a-zA-Z0-9|-]+)/([0-9]+).html should do. It's important to put the dash at the end of the character class.