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(",", "")
Related
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
I have multiple occurance of src={icons.ICON_NAME_HERE} in my code, that I would like to change to name="ICON_NAME_HERE".
Is it possible to do it with regular expressions, so I can keep whatever is in code as ICON_NAME_HERE?
To clarify:
I have for example src={icons.upload} and src={icons.download}, I want to do replace all with one regexp, so those gets converted to name="upload" and name="download"
Try searching on the following pattern:
src=\{icons\.([^}]+)\}
And then replace with your replacement:
name="$1"
In case you are wondering, the quantity in parentheses in the search pattern is captured during the regex search. Then, we can access that captured group using $1 in the replacement. In this case, the captured group should just be the name of the icon.
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
I want to write a regexp formula for the below sip message that takes number:
< sip:callpark#as1sip1.com:5060;user=callpark;service=callpark;preason=park;paction=park;ptoken=150009;pautortrv=180;nt_server_host=47.168.105.100:5060 >
(Actually there are "<" and ">" signs in the message, but the site does not let me write)
For this case, I want to select ptoken value.. I wrote an expression such as: ptoken=(.*);p but it returns me ptoken=150009;p, I just need the number:150009
How do I write a regexp for this case?
PS: I write this for XML script..
Thanks,
I SOLVE THE PROBLEM BY USING TWO REGEX:
ereg assign_to="token" check_it="true" header="Refer-To:" regexp="(ptoken=([\d]*))" search_in="hdr"/
ereg assign_to="callParkToken" search_in="var" variable="token" check_it="true" regexp="([\d].*)" /
You could use the following regex:
ptoken=(\d+)
# searches for ptoken= literally
# captures every digit found in the first group
Your wanted numbers are in the first group then. Take a look at this demo on regex101.com. Depending on your actual needs, there could be better approaches (Xpath? as tagged as XML) though.
You should use lookahead and lookbehind:
(?<=ptoken=)(.+?)(?=;)
It captures any character (.+?) before which is ptoken= and behind which is ;
The <ereg ... > action has the assign_to parameter. In your case assign_to="token". In fact, the parameter can receive several variable names. The first is assigned the whole string matching the regular expression, and the following are assigned the "capture groups" of the regular expression.
If your regexp is ptoken=([\d]*), the whole match includes ptoken which is bad. The first capture group is ([\d]*) which is the required value. Thus, use <ereg regexp="ptoken=([\d]*)" assign_to="dummyvar,token" ..other parameters here.. >.
Is it working?
I have to parse a lot of content with a regular expression.
The content might, for example, be:
14-08-2015 14:18 : Example : Hello =) How are you?
What are you doing?
14-08-2015 14:19: Example2 : I'm fine thanks!
I have this regular expression that will of course return 2 matches, and the groups that I need - data, hour, name, multi line message:
(\d{2}-\d{2}-\d{4})\s?(\d{2}:\d{2})\s?:([^:]+):([^\d]+)
The problem is that if a number is written inside the message this will not be OK, because the regex will stop getting more characters.
For example in this case this will not work:
14-08-2015 14:18 : Example : Hello =) How are you?
What are you 2 doing?
14-08-2015 14:19: Example2 : I'm fine thanks!
How do I get all the characters until a new date/hour is found?
The problem is with your final capturing group ([^\d]+).
Instead you can use ((?:(?!\d{2}-\d{2}-\d{4})[\s\S])+)
The outer parenthesis: ((?:(?!\d{2}-\d{2}-\d{4})[\s\S])+) indicate a capturing group
The next set of parenthesis: ((?:(?!\d{2}-\d{2}-\d{4})[\s\S])+) indicate a non-capturing group that we want to match 1 to infinite amount of times.
Inside we have a negative look ahead: ((?:(?!\d{2}-\d{2}-\d{4})[\s\S])+). This says that whatever we are matching cannot include a date.
What we actually capture: ((?:(?!\d{2}-\d{2}-\d{4})[\s\S])+) means we capture every character including a new line.
The entire regex that works looks like this:
(\d{2}-\d{2}-\d{4})\s?(\d{2}:\d{2})\s?:([^:]+):((?:(?!\d{2}-\d{2}-\d{4})[\s\S])+)
https://regex101.com/r/wH5xR2/2
Use a lookahead for dates and get everything up to that.
/^(\d{2}-\d{2}-\d{4})\s?(\d{2}:\d{2})\s?:([^:]+):\s?((?:(?!^\d{2}-\d{2}-\d{4}\s?\d{2}:\d{2}).)*)/sm
I've edited you regex in two ways:
Added ^to the front, ensuring you only start from timestamps on their own line, which should filter out most issues with people posting timestamps
Replaced the last capturing group with ((?:(?!^\d{2}-\d{2}-\d{4}\s?\d{2}:\d{2}).)*)
(?!^\d{2}-\d{2}-\d{4}\s?\d{2}:\d{2}) is a negative lookahead, with date
(?:(lookahead).)* Looks for any amount of characters that aren't followed by a date anchored to the start of a line.
((?:(lookahead).)*) Just captures the group for you.
It's not that efficient, but it works. Note the s flag for dotall (dot matches newlines) and m flag that lets ^ match at the start of line. ^ is necessary in the lookahead so that you don't stop the match in case someone posts a timestamp, and in the start to make sure you only match dates from the start of a line.
DEMO: https://regex101.com/r/rX8eH0/3
DEMO with flags in regex: https://regex101.com/r/rX8eH0/4