Posts

Showing posts with the label regex

Applying regex to pandas column based on different pos of same character

8 I have a dataframe like as shown below tdf = pd.DataFrame({'text_1':['value: 1.25MG - OM - PO/TUBE - ashaf', 'value:2.5 MG - OM - PO/TUBE -test','value: 18 UNITS(S)','value: 850 MG - TDS AFTER FOOD - SC (SUBCUTANEOUS) -had', 'value: 75 MG - OM - PO/TUBE']}) I would like to apply regex and create two columns based on rules given below col val should store all text after value: and before first hyphen col Adm should store all text after third hyphen I tried the below but it doesn't work accurately tdf['text_1'].str.findall('[.0-9]+\s*[mgMG/lLcCUNIT]+') python regex pandas string dataframe ...

Regex for single digit catches two digit numbers?

2 This regex catches two digit nubers: if I have y11 it will still catch it, how to match only y0, y1, y2, y3, y4, y5, y6, y7, y8, y9 ?? After the digit there might be a blank space or a letter or a dot. preg_replace('/y([1-9]{1})/', 'y0$1', $string) php regex Share Improve this question Follow asked Apr 5 at 10:43 ...

Java regex: Replace all characters with `+` except instances of a given string

I have the following problem which states Replace all characters in a string with + symbol except instances of the given string in the method so for example if the string given was abc123efg and they want me to replace every character except every instance of 123 then it would become +++123+++. I figured a regular expression is probably the best for this and I came up with this. str.replaceAll("[^str]","+") where str is a variable, but its not letting me use the method without putting it in quotations. If I just want to replace the variable string str how can I do that? I ran it with the string manually typed and it worked on the method, but can I just input a variable? as of right now I believe its looking for the string "str" and not the variable string. Here is the output its right for so many cases except for two :( List of open test cases: plusOut("12xy34", "xy") → "++xy++" plusOut("12xy34", "1") → ...