I have a lot of calls in lots of different files to os.getenv('some_var'). I would like to replace all of these with os.environ['some_var'].
I know how to replace all instances of os.getenv with os.environ but not how to replace the (.*) with [.*] without loosing the text inside.
Try this regex:
(os\.)[^()]*\(([^()]*)\)
Replace each match with \1environ[\2]
Click for Demo
Explanation:
(os\.) - matches os. and capture in group 1
[^()]*\( - matches 0+ occurrences of any character that is neither a ( nor ) follwed by (
([^()]*) - matches 0+ occurrences of any character that is neither a ( nor ). This substring is captured in Group 2
\) - matches )
You can match the text and capture the text inside parenthesis using this regex,
os.getenv\('([^']+)'\)
And replace it with os.environ['\1']
This regex basically has three parts,
os.getenv\(' - This literally matches os.getenv('
([^']+) - This captures whatever text is there in parenthesis and captures it in group1
'\) - This literally matches ')
Demo
Related
I have this text:
text1 without brackets
text2 (with brackets)
and I need two groups in every line:
group#1: text1 without brackets
group#2:
group#1: text2
group#2: with brackets
Here is a link for this example: regexr.com
Thanks for help!
You may use
^(.*?)(?:\s*\(([^()]*)\))?$
See the regex demo and the regex graph:
Details
^ - start of string
(.*?) - Group 1: any 0+ chars as ew as possible
(?:\s*\(([^()]*)\))? - an optional sequence of patterns that is tried at least once:
\s* - 0+ whitespaces
\( - a ( char
([^()]*) - Group 2: 0+ chars other than ( and )
\) - a ) char
$ - end of the string.
Try pattern: ([^(\n]+)(?:\n|\(([^)]+))
Explanation:
([^(\n]+) - first capturing group: match one or more characters other than ( or \n so it will match everything until opening bracket or newline character
(?:...) - used in order to make use of alternation and not create second capturing group
\n|\(([^)]+) - match newline or bracker ( and one or more characters other than closing bracket ) storing it into second capturing group.
Demo
I have been trying and reading many similar SO answers with no luck.
I need to remove parentheses in the text inside parentheses keeping the text. Ideally with 1 regex... or maybe 2?
My text is:
Alpha (Bravo( Charlie))
I want to achieve:
Alpha (Bravo Charlie)
The best I got so far is:
\\(|\\)
but it gets:
Alpha Bravo Charlie
You can use a regex like this:
(\(.*?)\((.*?)\)
With this replacement string:
$1$2
Regex demo
Update: as per ııı comment, since I don't know your full sample text I provide this regex in case you have this scenario
(\([^)]*)\((.*?)\)
Regex demo
From your post and comments, it seems you want to remove only the inner most parenthesis, for which you can use following regex,
\(([^()]*)\)
And replace with $1 or \1 depending upon your language.
In this regex \( matches a starting parenthesis and \) matches a closing parenthesis and ([^()]*) ensures the captured text doesn't contain either ( or ) which ensures it is the innermost parenthesis and places the captured text in group1, and whole match is replaced by what got captured in group1 text, thus getting rid of the inner most parenthesis and retaining the text inside as it is.
Demo
Your pattern \(|\) uses an alternation then will match either an opening or closing parenthesis.
If according to the comments there is only 1 pair of nested parenthesis, you could match:
(\([^()]*)\(([^()]*\)[^()]*)\)
( Start capturing group
\( Match opening parenthesis
[^()]* Match 0+ times not ( or )
) Close group 1
\( Match
( Capturing group 2
\([^()]*\) match from ( till )
[^()]* Match 0+ times not ( or )
) close capturing group
\) Match closing parenthesis
And replace with the first and the second capturing group.
Regex demo
So I am trying to create a regex expression for the following template.
"[alphaNumeric]String/String.xcl"
So
[a1B2c3]Hello/Hello.xcl would pass
a1B2c3]hello/hello.xcl fails
[a1B2c3]Hello/hello.xcl fails
[a1B2c3]hello/hello.xc fails
I have tried the following so far:
\[[\da-zA-Z]+\][a-z]+\/[a-z]+\.xcl$
How do I check if the middle strings are identical?
Use a backreference:
\[[a-zA-Z0-9]+\]([^/]+)/\1\.xcl
The term in parenthesis captures the first part of your path. We may then refer to it later in the regex using \1.
Depending on how you plan to use this regex, you might need optional starting and closing anchors (^ and $).
Demo
You may capture the part after brackets and use a backreference after /:
^\[[\da-zA-Z]+]([A-Za-z]+)\/\1\.xcl$
^^^^^^^^^^ ^^
See the regex demo
Details
^ - start of the string
\[ - a [
[\da-zA-Z]+ - 1+ alphanumeric chars
] - a ] char
([A-Za-z]+) - Capturing group 1: one or more letters
\/ - a slash
\1 - a backreference to capturing group 1 value
\.xcl - .xcl substring
$ - end of string.
NOTE: If you do not care about what kind of chars there can be inside brackets, you may replace [\da-zA-Z]+ with [^\]]+.
NOTE2: If you want to match any chars on both ends of /, replace ([A-Za-z]+) with ([^\/]+).
I'm trying to turn cast(("Sparkles"), GetBitmapData); to GetBitmapData("Sparkles");
I've got this for my find code:
cast\(\(\"\.*\"\),\ .*\);
but this replace doesn't work:
$2\(\"$1\"\);
What do I need to do to make this work?
You regex does not contain capturing groups and you try to access them with numbered backreferences. Besides, you escaped the dot, and \.* just matches 0+ dot symbols.
You may use the following regex replacement:
Find what: cast\(\("(.*?)"\),\s*(\w+)\);
Replace with: $2("$1");
Here is a .NET regex demo (FlashDevelop S&R feature uses .NET regex flavor).
Pattern details:
cast\(\(" - a cast((" substring
(.*?) - Group 1 (referred to with $1) capturing any 0+ chars as few as possible up to the first...
"\), - a "), substring
\s* - 0+ whitespaces
(\w+) - Group 2 (referred to with $2) capturing 1+ word chars (letters/digits/_)
\); - a ); substring.
Can't get why this regex (regex101)
/[\|]?([a-z0-9A-Z]+)(?:[\(]?[,][\)]?)?[\|]?/g
captures all the input, while this (regex101)
/[\|]+([a-z0-9A-Z]+)(?:[\(]?[,][\)]?)?[\|]?/g
captures only |Func
Input string is |Func(param1, param2, param32, param54, param293, par13am, param)|
Also how can i match repeated capturing group in normal way? E.g. i have regex
/\(\(\s*([a-z\_]+){1}(?:\s+\,\s+(\d+)*)*\s*\)\)/gui
And input string is (( string , 1 , 2 )).
Regex101 says "a repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations...". I've tried to follow this tip, but it didn't helped me.
Your /[\|]+([a-z0-9A-Z]+)(?:[\(]?[,][\)]?)?[\|]?/g regex does not match because you did not define a pattern to match the words inside parentheses. You might fix it as \|+([a-z0-9A-Z]+)(?:\(?(\w+(?:\s*,\s*\w+)*)\)?)?\|?, but all the values inside parentheses would be matched into one single group that you would have to split later.
It is not possible to get an arbitrary number of captures with a PCRE regex, as in case of repeated captures only the last captured value is stored in the group buffer.
What you may do is get mutliple matches with preg_match_all capturing the initial delimiter.
So, to match the second string, you may use
(?:\G(?!\A)\s*,\s*|\|+([a-z0-9A-Z]+)\()\K\w+
See the regex demo.
Details:
(?:\G(?!\A)\s*,\s*|\|+([a-z0-9A-Z]+)\() - either the end of the previous match (\G(?!\A)) and a comma enclosed with 0+ whitespaces (\s*,\s*), or 1+ | symbols (\|+), followed with 1+ alphanumeric chars (captured into Group 1, ([a-z0-9A-Z]+)) and a ( symbol (\()
\K - omit the text matched so far
\w+ - 1+ word chars.