I was trying to get all quoted (" or ') substrings from a string excluding the quotation marks.
I came up with this:
"((?:').*[^'](?:'))|((?:\").*[^\"](?:\"))"
For some reason the matching string still contains the quotation marks in it.
Any reason why ?
Sincerely, nikita.utiu.
You could do it with lookahead and lookbehind assertions:
>>> match = re.search(r"(?<=').*?(?=')", "a 'quoted' string. 'second' quote")
>>> print match.group(0)
quoted
Using non-capturing groups doesn’t mean that they are not captured at all. They just don’t create separate capturing groups like normal groups do.
But the structure of the regular expression requires that the quotation marks are part of the match:
"('[^']*'|\"[^\"]*\")"
Then just remove the surrounding quotation marks when processing the matched parts with matched_string[1:-1].
You could try:
import shlex
...
lexer = shlex.shlex(your_input_string)
quoted = [piece.strip("'\"") for piece in lexer if piece.startswith("'") or piece.startswith('"')]
shlex (lexical analysis) takes care of escaped quotes for you. Though note that it does not work with unicode strings.
Related
I need a regular expression able to match everything but a string starting with a specific pattern (specifically index.php and what follows, like index.php?id=2342343).
Regex: match everything but:
a string starting with a specific pattern (e.g. any - empty, too - string not starting with foo):
Lookahead-based solution for NFAs:
^(?!foo).*$
^(?!foo)
Negated character class based solution for regex engines not supporting lookarounds:
^(([^f].{2}|.[^o].|.{2}[^o]).*|.{0,2})$
^([^f].{2}|.[^o].|.{2}[^o])|^.{0,2}$
a string ending with a specific pattern (say, no world. at the end):
Lookbehind-based solution:
(?<!world\.)$
^.*(?<!world\.)$
Lookahead solution:
^(?!.*world\.$).*
^(?!.*world\.$)
POSIX workaround:
^(.*([^w].{5}|.[^o].{4}|.{2}[^r].{3}|.{3}[^l].{2}|.{4}[^d].|.{5}[^.])|.{0,5})$
([^w].{5}|.[^o].{4}|.{2}[^r].{3}|.{3}[^l].{2}|.{4}[^d].|.{5}[^.]$|^.{0,5})$
a string containing specific text (say, not match a string having foo):
Lookaround-based solution:
^(?!.*foo)
^(?!.*foo).*$
POSIX workaround:
Use the online regex generator at www.formauri.es/personal/pgimeno/misc/non-match-regex
a string containing specific character (say, avoid matching a string having a | symbol):
^[^|]*$
a string equal to some string (say, not equal to foo):
Lookaround-based:
^(?!foo$)
^(?!foo$).*$
POSIX:
^(.{0,2}|.{4,}|[^f]..|.[^o].|..[^o])$
a sequence of characters:
PCRE (match any text but cat): /cat(*SKIP)(*FAIL)|[^c]*(?:c(?!at)[^c]*)*/i or /cat(*SKIP)(*FAIL)|(?:(?!cat).)+/is
Other engines allowing lookarounds: (cat)|[^c]*(?:c(?!at)[^c]*)* (or (?s)(cat)|(?:(?!cat).)*, or (cat)|[^c]+(?:c(?!at)[^c]*)*|(?:c(?!at)[^c]*)+[^c]*) and then check with language means: if Group 1 matched, it is not what we need, else, grab the match value if not empty
a certain single character or a set of characters:
Use a negated character class: [^a-z]+ (any char other than a lowercase ASCII letter)
Matching any char(s) but |: [^|]+
Demo note: the newline \n is used inside negated character classes in demos to avoid match overflow to the neighboring line(s). They are not necessary when testing individual strings.
Anchor note: In many languages, use \A to define the unambiguous start of string, and \z (in Python, it is \Z, in JavaScript, $ is OK) to define the very end of the string.
Dot note: In many flavors (but not POSIX, TRE, TCL), . matches any char but a newline char. Make sure you use a corresponding DOTALL modifier (/s in PCRE/Boost/.NET/Python/Java and /m in Ruby) for the . to match any char including a newline.
Backslash note: In languages where you have to declare patterns with C strings allowing escape sequences (like \n for a newline), you need to double the backslashes escaping special characters so that the engine could treat them as literal characters (e.g. in Java, world\. will be declared as "world\\.", or use a character class: "world[.]"). Use raw string literals (Python r'\bworld\b'), C# verbatim string literals #"world\.", or slashy strings/regex literal notations like /world\./.
You could use a negative lookahead from the start, e.g., ^(?!foo).*$ shouldn't match anything starting with foo.
You can put a ^ in the beginning of a character set to match anything but those characters.
[^=]*
will match everything but =
Just match /^index\.php/, and then reject whatever matches it.
In Python:
>>> import re
>>> p='^(?!index\.php\?[0-9]+).*$'
>>> s1='index.php?12345'
>>> re.match(p,s1)
>>> s2='index.html?12345'
>>> re.match(p,s2)
<_sre.SRE_Match object at 0xb7d65fa8>
Came across this thread after a long search. I had this problem for multiple searches and replace of some occurrences. But the pattern I used was matching till the end. Example below
import re
text = "start![image]xxx(xx.png) yyy xx![image]xxx(xxx.png) end"
replaced_text = re.sub(r'!\[image\](.*)\(.*\.png\)', '*', text)
print(replaced_text)
gave
start* end
Basically, the regex was matching from the first ![image] to the last .png, swallowing the middle yyy
Used the method posted above https://stackoverflow.com/a/17761124/429476 by Firish to break the match between the occurrence. Here the space is not matched; as the words are separated by space.
replaced_text = re.sub(r'!\[image\]([^ ]*)\([^ ]*\.png\)', '*', text)
and got what I wanted
start* yyy xx* end
I would like to strip all of the the punctuations (except the dot) from the beginning and end of a string, but not in the middle of it.
For instance for an original string:
##%%.Hol$a.A.$%
I would like to get the word .Hol$a.A. removed from the end and beginning but not from the middle of the word.
Another example could be for the string:
##%%...&Hol$a.A....$%
In this case the returned string should be ..&Hol$a.A.... because we do not care if the allowed characters are repeated.
The idea is to remove all of the punctuations( except the dot ) just at the beginning and end of the word. A word is defined as \w and/or a .
A practical example is the string 'Barnes&Nobles'. For text analysis is important to recognize Barnes&Nobles as a single entity, but without the '
How to accomplish the goal using Regex?
Use this simple and easily adaptable regex:
[\w.].*[\w.]
It will match exactly your desired result, nothing more.
[\w.] matches any alphanumeric character and the dot
.* matches any character (except newline normally)
[\w.] matches any alphanumeric character and the dot
To change the delimiters, simply change the set of allowed characters inside the [] brackets.
Check this regex out on regex101.com
import re
data = '##%%.Hol$a.A.$%'
pattern = r'[\w.].*[\w.]'
print(re.search(pattern, data).group(0))
# Output: .Hol$a.A.
Depending on what you mean with striping the punctuation, you can adapt the following code :
import re
res = re.search(r"^[^.]*(.[^.]*.([^.]*.)*?)[^.]*$", "##%%.Hol$a.A.$%")
mystr = res.group(1)
This will strip everything before and after the dot in the expression.
Warning, you will have to check if the result is different of None, if the string doesn't match.
I'm building a regex that must match a certain pattern that starts with a specific symbol, but at the same time it must not match a pattern that starts with two or more occurrences of that same specific symbol.
To elaborate better, this is my scenario. I have a string like this:
Hello %partials/footer/mail,
%no_slashes_here
%{using_braces}_here
%%should_not_be_matched
And I'm trying to match those substrings that start with exactly one % symbol (since in my case a double %% means "escaping" and should not be matched) and they could optionally be surrounded by curly braces. And at the end, I need to capture the matched substrings but without the % symbol.
So far my regular expression is:
%\{*([0-9a-zA-Z_/]+)\}*
And the captured matches result is:
partials/footer/mail
no_slashes_here
using_braces
should_not_be_matched
Which is very close to what I need, but I got stuck into the double %% escaping part. I don't know how to negate two or more % symbols at the beginning and at the same time allow exactly one occurrence at the beginning too.
EDIT:
Sorry that I missed that, I'm using python.
With negative lookbehind:
%(?<!%%)\{*([0-9a-zA-Z_\/]+)\}*
Regex 101
If this is line based -- you can do:
(?:^|[^%])%\{?([^%}]+)\}?
Demo
Python demo:
txt='''\
Hello %partials/footer/mail,
%no_slashes_here
%{using_braces}_here
%%should_not_be_matched
This %% niether'''
import re
for line in txt.splitlines():
m=re.search(r'(?:^|[^%])%\{?([^%}]+)\}?', line)
if m:
print m.group(1)
It is unclear from your question how % this % should be treated
What about
(?<=%)([^%]+)
Regex101 demo
I've assumed PCRE, as you've not declared which flavour of Regex you're using.
I have a string:
test_str = 'This is the string and it "contains {0} a" few {1} sets of curly brackets'
I would like to only find {0} and not {1} in this example, that is, the brackets themselves and their contents, if only within a set of double quotes.
I've started to solve this by matching the portion in double quotes:
(?<=").*(?=")
See https://regex101.com/r/qO0pO2/1
but I am having difficulty only matching the {0} portion
How can I extend this regex to match {0}?
Remove the pipe | it will work great: Live Demo
And here is for multiple char between {}
(?<=)\{[^\}]*\}(?=)
With Live Demo
Update:
This does the stuff :
".*({[^\}]*\}).*"
If the quotes are balanced, you could use a lookahead to check for an uneven amount ahead. If you know, that there is only one quoted substring, check if there occurs only one " until the end $
{[^}]+}(?=[^"]*"[^"]*$)
See demo. But if there could be any amount of quoted parts check for an uneven amount until end.
{[^}]+}(?=[^"]*"(?:[^"]*"[^"]*")*[^"]*$)
{[^}]+} matches the braced stuff: literal { followed by [^}]+ one or more non} until }
[^"]*" inside the lookahead matches until the first quote
(?:[^"]*"[^"]*")* followed by zero or more balanced, preceded by any amount of non quotes
[^"]*$ followed by any amount of non quotes until end
See demo at regex101
You can try word boundary \B and lookarounds- i.e.
>>>test_str="This is the string and it contains {0} a few {1} sets of curly brackets"
>>>re.findall(r'(?<=\B){.*?}(?=\B)',test_str)
>>>['{0}', '{1}']
See live DEMO
But if your string does not have word boundary then try lazy quantifier evaluation
>>>test_str="This is the string and it contains {0} a few {1} sets of curly brackets"
>>>re.findall(r'{.*?}',test_str)
>>>['{0}', '{1}']
See live DEMO
EDIT
If you want only {0} then you have to use escape character(\) before braces, since braces are regex token-try as below.
>>>test_str="This is the string and it contains {0} a few {1} sets of curly brackets"
>>>re.findall(r'\{0\}',test_str)
>>>['{0}']
Might be difficult to do in one regex, but it's easy with two:
from re import findall
# First find all quoted strings...
for quoted in findall(r'"[^"]*"', test_str):
# ...then find all bracketed expressions
for match in findall(r'\{[^\}]*\}', quoted):
print(match)
or as a one-liner:
[match for match in findall(r'\{[^\}]*\}', quoted) for quoted in findall(r'"[^"]*"', test_str)]
I am having some trouble wrapping my head around Python regular expressions to come up with a regular expression to extract specific values.
The page I am trying to parse has a number of productIds which appear in the following format
\"productId\":\"111111\"
I need to extract all the values, 111111 in this case.
t = "\"productId\":\"111111\""
m = re.match("\W*productId[^:]*:\D*(\d+)", t)
if m:
print m.group(1)
meaning match non-word characters (\W*), then productId followed by non-column characters ([^:]*) and a :. Then match non-digits (\D*) and match and capture following digits ((\d+)).
Output
111111
something like this:
In [13]: s=r'\"productId\":\"111111\"'
In [14]: print s
\"productId\":\"111111\"
In [15]: import re
In [16]: re.findall(r'\d+', s)
Out[16]: ['111111']
The backslashes here might add to the confusion, because they are used as an escape character both by (non-raw) Python strings and by the regexp syntax.
This extracts the product ids from the format you posted:
re_prodId = re.compile(r'\\"productId\\":\\"([^"]+)\\"')
The raw string r'...' does away with one level of backslash escaping; the use of a single quote as the string delimiter does away with the need to escape double quotes; and finally the backslashe are doubled (only once) because of their special meaning in the regexp language.
You can use the regexp object's findall() method to find all matches in some text:
re_prodId.findall(text_to_search)
This will return a list of all product ids.
Try this,
:\\"(\d*)\\"
Give more examples of your data if this doesn't do what you want.