I have the following regex and the input string.
pattern = re.compile(r'\s+(?=[^()|^{}|^<>]*(?:\(|\{|\<|$))')
string = "token1 token2 {a | op (b|c) | d}"
print pattern.split(string)
the result is : ["token1","token2","{a | op","(b|c) |d}"]
I want the regex to give the following result : ["token1","token2","{a | op (b|c) | d}"]
string = "token1 token2 {a | op (b|c) | d}"
re.findall(r'\w+|\{.*}',string)
output:
['token1', 'token2', '{a | op (b|c) | d}']
You can simply split by this
\s+(?![^{]*\})
See demo.
https://regex101.com/r/WjQVqZ/1
The raw pattern to use with the split method is r'\s+(?=[^\}]*(?:\{|$))'.
Every time whitespace is encountered, you want to look ahead for a closing curly brace, so you know if the white space is inside of braces - unless an opening curly brace or the end of the string is seen next.
Related
input strings:
"| VLAN56 | LAB06 | Labor 06 | 56 | 172.16.56.0/24 | VLAN56_LAB06 | ✔️ | |",
"| VLAN57 | LAB07 | Labor 07 | 57 | 172.16.57.0/24 | VLAN57_LAB07 | ✔️ | ##848484: |"
regex:
'\|\s+(\d+).+(VLAN\d+_[0-9A-Za-z]+)\s+\|.+(#[0-9A-Fa-f]{6})?'
The goal is to get the VLAN number, hostname, and if there is one, the color code, but with a "?" it ignores the color code every time, even when it should match.
With the "?" the last capture group is always None.
You may use this regex:
\|\s+(\d+).+(VLAN\d+_[0-9A-Za-z]+)\s+\|[^|]+\|[^#|]*(#[0-9A-Fa-f]{6})?
You have a demo here: https://regex101.com/r/SWe42v/1
The reason why it didn't work with your regex is that .+ is a greedy quantifier: It matches as much as it can.
So, when you added the ? to the last part of the regex, you give no option to backtrack. The .+ matches the rest of the string/line and the group captures nothing (which is correct because it is optional)
In order to fix it, you can simply try to match the column with the emoji. You don't care about its content, so you simply use |[^|]+to skip the column.
This sort of construct is widely used in regexes: SEPARATOR[^SEPARATOR]*
The reason why the last capture group is None is that the preceding .+ can capture the rest of the line.
I would however first use the fact that this is a pipe-separated format, and split by that pipe symbol and then retrieve the elements of interest needed by slicing them from that result by their index:
import re
s = "| VLAN57 | LAB07 | Labor 07 | 57 | 172.16.57.0/24 | VLAN57_LAB07 | ✔️ | ##848484: |"
vlan,name,color = re.split(r"\s*\|\s*", s)[4:9:2]
print(vlan, name, color)
This code is in my opinion easier to read and to maintain.
I think this is what you're after: Demo
^\|\s+(VLAN[0-9A-Za-z]+)\s+\|\s+([0-9A-Za-z]+)\s+\|.*((?<=\#)[0-9A-Fa-f]{6})?.*$
^\|\s+ - the start of the line must be a pipe followed by some whitespace.
(VLAN[0-9A-Za-z]+) - What comes next is the VLAN - so we capture it; with the VLAN and all (at least 1) following alpha-numeric chars.
\s+\|\s+ - there's then another pipe delimeter, with whitespace either side.
([0-9A-Za-z]+) - the column after the vlan name is the device name; so we capture the alphanumeric value from that.
\s+\| - after our device there's more whitespace and then the delimiter
.* - following that there's a load of stuff that we're not interested in; could be anything.
((?<=\#)[0-9A-Fa-f]{6})? - next there may be a 6 hex char value preceded by a hash; we want to capture only the hex value part.
(...) says this is another capture group
(?<=\#) is a positive look behind; i.e. checks that we're preceded by some value (in this case #) but doesn't include it within the surrounding capture
[0-9A-Fa-f]{6} is the 6 hex chars to capture
? after the parenthesis says there's 0 or 1 of these (i.e. it's optional); so if it's there we capture it, but if it's not that's not an issue.
.*$ says we can have whatever else through to the end of the string.
We could strip a few of those bits out; or add more in (e.g. if we know exactly what column everythign will be in we can massively simplify by just capturing content from those columns. E.g.
^\|\s*([^\|\s]+)\s*\|\s*([^\|\s]+)\s*\|\s*[^\|]*\s*\|\s*[^\|\s]*\s*\|\s*[^\|\s]*\s*\|\s*[^\|\s]*\s*\|\s*[^\|\s]*\s*\|\s*[^\|\d]*(\d{6})?[^\|]*\s*\|$
... But amend per your requirements / whatever feels most robust and suitable for your purposes.
I'm sure this will be easy pickings for more experienced programmers than I, but this problem is bedeviling me and I've made a couple of failed attempts, so I wanted to see what other people might come up with.
I have about a hundred strings that look something like this:
(argument1 OR argument2) | inputlookup my_lookup.csv | `macro1(tag,bunit)` | `macro2(category)` | `macro_3(tag,\"expected\",category)` | `macro4(tag,\"timesync\")`
The goal is to find the arguments to the macro function and replace them with the count of the arguments, so that the final output looks like this:
(argument1 OR argument2) | inputlookup my_lookup.csv | `macro1(2)` | `macro2(1)` | `macro_3(3)` | `macro4(2)`
Python has ways of obtaining the count I need (I was simply counting up the number of commas in a string and adding 1), and Python has plenty of regex-type solutions for inline string replacement, but for the life of me I can't figure out how to combine them.
It seems something like re.sub won't let me identify a substring, count the number of commas in the substring, and then replace the substring with that value (unless I am missing something in the docs).
Can anybody think of a way to do this? Have I missed something obvious?
Solution:
import re
def count_commas(input_str):
c = 0
for s in input_str:
if s == ',':
c += 1
return c
pattern = r'\([A-Za-z0-9,""]+\)'
original_str = '(argument1 OR argument2) | inputlookup my_lookup.csv | `macro1(tag,bunit)` | `macro2(category)` | `macro_3(tag,\"expected\",category)` | `macro4(tag,\"timesync\")`'
matches = re.findall(pattern, original_str)
for match in matches:
comma_count = count_commas(match) + 1
match = match.replace('(', '\(').replace(')', '\)')
original_str = re.sub(r'' + match, '(' + str(comma_count) + ')', original_str)
print (original_str)
Explanation:
pattern : "\([A-Za-z0-9,""]+\)" - backslashes to escape the special characters '(' and ')' in regex, and then I am looking for alphanumeric, comma and quotations (in the square-brackets) which is followed with '+' which means one or more than one repetition of such symbols in the square brackets.
matches : list of all the matches found. Eg - (tag,bunit)
Then, I am looping over all the matches to find the number of commas in the match, followed by replacing the '(' with '\(' and ')' with '\)' so as to escape in regex.
Finally, in the last line of the loop, I am using re.sub to replace the matched string with the comma count in the original string.
I have a bunch of strings that look like this
s = '| this is | my | made up string '
I'd like to write a function that removes all the whitespace immediately preceeding and following the |'s. So running myFunc(s, '|') would return
'|this is|my|made up string '
Obviously strip() is too powerful, as I'd like to respect some of the whitespace. How can I do this in python?
You can split the the string at | then trim each substring and then join it again:
'|'.join([i.strip() for i in s.split('|')])
Use regex replace.
import re
s = '| this is | my | made up string '
print(re.sub(r'\s*\|\s*', '|', s))
Will give this output -
'|this is|my|made up string '
I have a string
k1|v1|k2|v2|k3|v3|k4|v4
and I want to match on every other | so I can change the string to
k1:v1|k2:v2|k3:v3|k4:v4
I know I can match on | by doing a grouping like (|) but I can't figure out how to match only every other pipe.
Thanks.
Match with:
([^|]*)\|([^|]*(\||$))
Replace with $1:$2.
See it in action
General idea:
[^|]* - multiple non-| characters
() defines a group
(\||$) - a | or the end of the string
The entire regex reads as multiple non | characters in the first group, followed by a |, followed by multiple non | characters and a | or end of string in the second group
I want to unstruct wikipedia synonym bracket.
Here's a easy one to do.
He is [[Korean]].
I can remove bracket.
Here's another difficult one.
He lives in [[Gimhae city|Gimhae]].
The first one(Gimhae city) is wikipedia document title.
So I have to get second one in bracket.
Any suggestion is welcome.
You can use the following regex:
\[{2}(?:[^|\]]*\|)?([^]]*)]{2}
And relace with \1.
See demo
Here is what the regex matches:
\[{2} - 2 opening square brackets
(?:[^|\]]*\|)? - 0 or 1 sequence of characters other than | and ] (with [^|\]]*) and a literal | with \| (note it is escaped outside of character class)
([^]]*) - matches and captures into Group 1 that we'll reference later with \1 0 or more characters other than a closing square bracket
]{2} - 2 closing square brackets (note we do not have to escape them here since the first [ was escaped).
The Python snippet:
import re
p = re.compile(r'\[{2}(?:[^|\]]*\|)?([^]]*)]{2}')
test_str = "He lives in [[Gimhae city|Gimhae]]. He lives in [[Gimhae]]. "
result = re.sub(p, r"\1", test_str)
print(result) # => He lives in Gimhae. He lives in Gimhae.