Regex expression to extract strings - regex

I have the following expression:
MN=Abc123,MN=sssa,MN=abc adsa 1,MN=&3ams d'amé,MN=dat,CB=ds,CB=ds
How can I extract one by one the expressions following MN= ?
eg: firstly I want to extract Abc123, secondly I wnat sssa and so on ...
Appreciate your answer!

Use a capture group:
"[A-Z]{2}=([^,]+)"
Then get the first group form your matched object.
Or if the language you are dealing for supports the look-around you can use a positive look-behind in order to directly match the expected parts:
"(?<=[A-Z]{2}=)[^,]+"

If your regex environment supports lookbehind, you can extract desired information with this regex:
Environment supporting Lookbehind
(?<=MN=)(.*?)(?=,)
Environment not supporting Lookbehind
(?:MN=)(.*?)(?=,)
Your desired result will be stored in Group 1, aka $1
Based on your input string, here is result
Abc123
sssa
abc adsa 1
&3ams d'amé
dat
See live demo here

Related

Regex - How to match on all text inside of ${}

I need a regular expression to match all text inside of ${}.
i.e:
For the following case:
Hello, {does this work ${yes} I hope} so and this is another ${case}
It should match on "yes" and "case".
Use the following pattern:
\$\{(.*?)\}
Or, if your regex engine/flavor does not support capture groups but does support lookarounds, you may use:
(?<=\$\{).*?(?=\})

how to make a regular expression for this?

I want to make a regular expression on the string "{{c1::tiger}} is
a kind of {{c2::animal::something movable}}" to get the word "tiger" and "animal",and I have made this expression \{\{c\d+::((?P<value>.*?)(:{0,2})(.*?))\}\},also I want to use group('value') to achieve this.The result word "tiger" is exactly what I need,but always get the wrong result "animal::something movable"(which I mean "animal"),could anyone help me to solve this problem?Thanks.
The pattern that you tried contains 4 capturing groups and for the current example data group 1 and group 3 are empty.
To get tiger you could use a single capturing group with a negated character class:
\{\{c\d+::(?P<value>.*?)(?:::|}})
Regex demo
If the closing }} have to be present, you could use:
\{\{c\d+::(?P<value>.*?)(?:::.*)?}}
Regex demo
This would work for your example String:
c1::(?P<valueTiger>[a-z]*).*?c2::(?P<valueAnimal>[a-z]*)
Regex101
\{\{c\d+::(?P<value>[^:}]+)(?::{0,2}([^}]+))?\}\}
Demo

Regular Expression to return number without coma

I have to extract a number formatted xx,xxx.xx in a different format - xxxxx.xx by applying a regular expression. In other words, I have to remove the comma from the number in the final capture group.
I am not quite sure if it's possible to achieve only with the regular expression and without writing specific code to split and join at these values.
Here is the demo.
This is the part of input string:
AMT : EGP 3,000.00
My current regex is AMT\s*:\s*EGP\s*(\d*,\d*.\d*), which basically retreives 3,000.00.
I'm expecting to have 3000.00 in final capture group.
EDIT:
Since the OP doesn't want to capture and replace, the following can be done:
AMT\s*:\s*EGP\s*(\d*),(\d*.\d*)
The expected data is now part of the two capturing groups, and can be accessed by concatenating them: \1\2.
Demo
You can capture everything other than the , in two groups, and then replace:
Capture with:
(AMT\s*:\s*EGP\s*\d*),(\d*.\d*)
Replace with: \1\2
Demo
Try this:
AMT\s*:\s*EGP\s*\K\d+(,\d{3})*(\.\d+)?
Here is Demo
After find the match, do something like: Mystring.Replac(",", "")

regex to select only the zipcode

,Ray Balwierczak,4/11/2017,,895 Forest Hill Rd,Apalachin,NY,13732,y,,
i want to select only 13732 from the line. I came up with this regex
(\d)(\s*\d+)*(\,y,,)
But its also selecting the ,y,, .if i remove it that part from regex, the regex also gets valid for the date. please help me on this.
Generally, if you want to match something without capturing it, use zero-length lookaround (lookahead or lookbehind). In your case, you can use lookahead:
(\d)(\s*\d+)*(?=\,y,,)
The syntax (?=<stuff>) means "followed by <stuff>, without matching it".
More information on lookarounds can be found in this tutorial.
Regex: \D*(\d{5})\D*
Explanation: match 5 digits surrounded by zero or more non-digits on both sides. Then you can extract group containing the match.
Here's code in python:
import re
string = ",Ray Balwierczak,4/11/2017,,895 Forest Hill Rd,Apalachin,NY,13732,y,,"
search = re.search("\D*(\d{5})\D*", string)
print search.group(1)
Output:
13732

Regular expression to match particular starting word or nothing

I'm struggling to come up with the correct regex for the following scenario.
Let's say you have to match a word either starts with http- or nothing
eg : http-test-data, test-data should be a match but xyz-test-data shouldn't be a match
the regex i came up so far is
(?:http-)?(test-data)
but it matches xyz-test-data as well.
You could simply use the following:
(?:http-|^)(test-data)
This tests for either a positive look-behind of http- or for the beginning of the string before test-data.
For example, for the sample data as follows:
http-test-data
xyz-test-data
http-test-data
xyz-test-data
test-data
yes-yes-test-data
-test-data
It yeilds:
http-test-data
http-test-data
test-data
Try this representation
^(http-|)(test-data)
Yes because there is a ? on the (?:http-). Then the regex will also match any string that contains test-data.