Python re.sub to capture groups unexpected behaviour [duplicate] - python-3.x

This question already has answers here:
Regular expression to stop at first match
(9 answers)
Python non-greedy regexes
(7 answers)
Closed 2 years ago.
Consider following code:
import re
mystring = "\enquote {aaa} a \enquote {bbb}"
text = re.sub(r"\\enquote \{(.+)\}", r"\1", mystring)
print(text)
is outputing:
"aaa} a \enquote {bbb"
but I am acrually trying to achieve output of:
"aaa a bbb"
What have I misunderstood?
Background: I am trying to do some simple conversion from LaTeX format to general text, for which I need to replace LaTeX commands, but retain raw text itself (and sometimes do also some actual replacing). So how can I do group capture in python?

Related

Python print like function for converting *args to a string [duplicate]

This question already has answers here:
Python convert tuple to string
(5 answers)
Closed 2 years ago.
I would like to know how to convert an unknown number of arguments to a string. what I need to do is redirect the output of a function that is supposed to go to print() to a Qt QPlainTextEdit. since it only accepts strings, I need a way to convert the given arguments to a string.
basically what I am looking for is print() but instead of outputting to the terminal, the output is to a string.
If you have an array of elements you can do:
def print_foo(arr,count):
for i in range(0,int(count)):
print(str(arr[i]))
Basically, converting to string is - newstr = str(varriable)

Replacing every 'a' for '&' [duplicate]

This question already has answers here:
How to use string.replace() in python 3.x
(9 answers)
Closed 5 years ago.
I am trying to write a program that replaces every 'a' for '&'.I am currently stumped and was wondering if someone could write it for me to get an idea of what it would look like. Thanks.
strw = "kabhi kabhi mery dil main khyaal aata hy"
print (strw.replace("a", "&"))

Regex for "anything between" 2 patterns [duplicate]

This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 5 years ago.
I have a string with multiple URLs and other content in it.
I have URLs in following format:
http://www.example.com/bla1/bla2/thumb_my-file-name(2).JPG
The part till http://www.example.com/bla1/bla2/thumb_ is fixed.
However, I dont know what will be: my-file-name(2). I know it will be ending with .JPG
So far, I'm using the following regex to detect them:
/http:\/\/www.example.com\/bla1\/bla2\/thumb_/g
and I have also used \.JPG to detect the end.
But how do I capture the middle part as well?
How do I catch this whole URL?
Try this regex pattern:
http://www.example.com/bla1/bla2/thumb_(.*?)\.JPG
Code:
var url = "http://www.example.com/bla1/bla2/thumb_my-file-name(2).JPG";
var myRegexp = /http:\/\/www.example.com\/bla1\/bla2\/thumb_(.*?)\.JPG/g;
var match = myRegexp.exec(url);
console.log(match[1]); // my-file-name(2)

How to use to get substring according to pattern in shell script without XML parser [duplicate]

This question already has answers here:
Extraction of data from a simple XML file
(11 answers)
Closed 7 years ago.
Below is the command used for getting the required string from the file.
cat xmlconfig.new |grep "id=\"build\""
< parameter id="build" >v6.0.9< /parameter >
I want the v6.0.9 value from the above result, in such a way that it tokenizes according to the bracket, not according to word position
I do not have xml parser installed on my machine and I do not have admin access
I want to know if it can be done without using XML parser
Use a proper XML parser, e.g. xsh:
open xmlconfig.new ;
echo //parameter[#id='build'] ;

Split string in Lua, save in 2 variables [duplicate]

This question already has answers here:
Split string in Lua?
(18 answers)
Closed 9 years ago.
I have one string, that is = s = "Pedro Martinez, Defense"
I want to split the string before the comma and after the comma, store those cuts on 2 variables, for example:
I think that I need to use the string.gmatch function or string.sub
How can I do that?
The accepted answer at How to convert GPS coordinates to decimal in Lua? shows how to use string.match and patterns. Applying the same techniques as mentioned there, you could use
local name, expertise = string.match(s, "(.*),%s*(%a*)")
which would lead to name being "Pedro Martinez" and expertise being "Defense".

Resources