Regular Expression to Match Specific "Values" in Isolated Group - regex

I have this regular expression to test
(\&TRUNC)[\(]{1,}(.+)[\)]{1,}
And I have this "tester"
((((&TRUNC((1800,000 / 510)) * 510) * 920) + (2 * (510 * 700)) + ((&TRUNC((1800,000 / 510)) - 1) * 2 * 510 * 80)) / 1000000) * 85,715
My expected value is (inside the personal command "&TRUNC(command)")
(1800,000 / 510)
I got this value
1800,000 / 510)) * 510) * 920) + (2 * (510 * 700)) + ((&TRUNC((1800,000 / 510)) - 1) * 2 * 510 * 80)) / 1000000
How can I get only expected value in a separated group?
PS:. The expressions inside the command called for me as "&TRUNC(command)" is variable.

In your regex
(\&TRUNC)[\(]{1,}(.+)[\)]{1,}
change .+ to make it not greedy .+?
(\&TRUNC)[\(]{1,}(.+?)[\)]{1,}
You can also simplify a bit
&TRUNC\(+(.+?)\)+

With SED, you can use back reference to match the text you are looking for -
[jaypal~/Temp]$ cat input_file
((((&TRUNC((1800,000 / 510)) * 510) * 920) + (2 * (510 * 700)) + ((&TRUNC((1800,000 / 510)) - 1) * 2 * 510 * 80)) / 1000000) * 85,715
[jaypal~/Temp]$ sed 's/.[^(&TRUNC)]*(*\&TRUNC((\(.[^*)]*\)))* \* .*/\1/' input_file
1800,000 / 510
Sorry, I dont know .NET but how about this one -
([\(]{1}[0-9,/ ]+[\)]{1})

Related

Regex of Visual Source Safe History

i'm stuck to write a regex pattern to the below lines, my problem is to match it in multilines
* History of Modifications:
* $Log: /System/Utilities.cpp $
*
* 5 7/12/22 4:49p Peter
*
* 4 5/12/22 5:57p Mina
*
* 3 20/11/22 6:15p Simon
*
* 2 18/10/22 1:48p Micheal
*
* 1 5/10/21 4:13p Peter
*
*/
these lines always start with
$log: filename $
until the comment ends with */
i tried these two regex
^ \* \d+ .*
/\d{1,2}\/\d{1,2}\/\d{2} \d{1,2}:\d{2}[ap] [A-Z][a-z]+/

Powershell replacing 2nd item in cron schedule?

Am sure I'm way off here & there's probably an easier way to do this, but I have to change some cron text schedules & change the hour in which they run..
The cron itself is in a text file so I was thinking of using powershell to get the content, update it & save it.. but I'm not very good with regex etc with this. -I've cobbled something together from internet sources, but something is still slightly missing....
I can do all the grabbing of the file content & writing etc, it's just the slightly more advanced replacement of the 2nd variable that's an issue....
Here's my code:
$testText = "cron(0 20 * * ? *)"
$pattern = '(\().+?(\))'
$new1 = [regex]::Matches($testText, $pattern).Value
"new1 $new1"
$new2 = $($new1 -replace '\s+', ' ').split()
"new2 $new2"
$new3 = $new2[1]-1 #Set back an hour
#$new3 = $new2[1] -replace $new2[1],"22" #Specify Hour
"new3 $new3"
$new4 = $testText -Replace($new2,$new3)
"new4 $new4"
This results in this output - where annoyingly the first bit is knocked off..
new1 (0 20 * * ? *)
new2 (0 20 * * ? *)
new3 19
new4 cron(19* * ? *)
I've split this using spaces as the 1st item after the parenthesis can be 1 or 2 digits & after the 2nd set of chars(the hour) these can also be different.. i.e. cron(30 06 ? * MON-FRI *)
Any help would be appreciated... - it's been a long day & my brain is very tired!!
$testText = "cron(0 20 * * ? *)"
$cron = $testText.split(' ').split('(').split(')')
'cron(' +
$cron[1] + ' ' +
(([datetime]::Today).AddHours($cron[2] -1).Hour) + ' ' +
$cron[3] + ' ' +
$cron[4] + ' ' +
$cron[5] + ' ' +
$cron[6] +
')'
or perhaps a little easier
$testText = "cron(0 20 * * ? *)"
$cron = $testText.split(' ').split('(').split(')')
$cron[2] = ([datetime]::Today).AddHours($cron[2]-1).Hour
'cron(' + ($cron -join ' ') + ')'

pattern with at least 8 characters/uppercase/number

currently I'm using symfony2 and I need a pattern with condition:
-min 8 characters, max 20 characters
-must contain 1 letter at least
-must contain 1 number at least
-may contain special characters like !##$%^&*()_+
I tried this code but it doen't work :
/**
* Encrypted password. Must be persisted.
* #Assert\Regex(
* pattern = "/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[?!-‌​/_/=:;§]).{8,20}+$/i",
* htmlPattern="/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[?!-‌​/_/=:;§]).{8,20}$/",
* match=true,
* message="message error ")
*
* #var string
*/
protected $password;
did you try something like that?
/**
* #Assert\Regex(
* pattern = "/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9!##$%^&*()_]+){8,20}$"/",
*
* rest of options...
*
* )
*/
Thank yoou so much for your answer .
I had to override regex for $plainPassword instead of $password because I'm using FOSuserBundle .

Complicated pricing regex with multipliers and various formats

I'm looking for a regex that looks for prices in a comment line that can be formatted differntly (depending on the person who entered them
REG / SZ / 236,30 SUMMER
should match 236.30 (rather easy)
WB / SZ / 187.75 EBS
should match 187.75 (could have done that by myself so far)
here are the tricky ones
FS / EBS / 1*145.80 + 231.30
FS / EBS / 1x 145,80 + 231
FS / EBS / 3x 145.80 + 4x231
FS / EBS / 3* 145.80 + 4x231
First should match 145.80 and 231.30
Second should match 145.80 and 231.00
Third should match 145.80 and 231 and possibly "4x" and "3x"
Fourth as third with * AND x
Is there any way to do that with a regex?
//EDIT (clarification)
I want to have a total sum in the end. So third and fourth case would be (3*145.80) + (4*231). Second case is intentionally 145,80 instead of 145.80.
What I got so far
(([0-9])*?\.([0-9])*)|(([0-9])*?\,([0-9])*)
Which will give me 236,30, 187.75, 145.80, 145,80
from re import findall
examples = ('REG / SZ / 236,30 SUMMER', \
'WB / SZ / 187.75 EBS', \
'FS / EBS / 1*145.80 + 231.30', \
'FS / EBS / 1x 145,80 + 231', \
'FS / EBS / 3x 145.80 + 4x231', \
'FS / EBS / 3* 145.80 + 4x231')
for line in examples:
numbers = findall(r'[/+]\s*(?:(\d+[.,]?\d*)[*x ]\s*)?(\d+[.,]?\d*)', line)
result = 0.0
for multiplier, value in numbers:
if not multiplier:
result += float(value.replace(',', '.'))
else:
result += float(multiplier) * float(value.replace(',', '.'))
print '%s\nAfter regex: %s\nResult: %.2f\n' % (line, numbers, result)
Produces the result:
REG / SZ / 236,30 SUMMER
After regex: [('', '236,30')]
Result: 236.30
WB / SZ / 187.75 EBS
After regex: [('', '187.75')]
Result: 187.75
FS / EBS / 1*145.80 + 231.30
After regex: [('1', '145.80'), ('', '231.30')]
Result: 377.10
FS / EBS / 1x 145,80 + 231
After regex: [('1', '145,80'), ('', '231')]
Result: 376.80
FS / EBS / 3x 145.80 + 4x231
After regex: [('3', '145.80'), ('4', '231')]
Result: 1361.40
FS / EBS / 3* 145.80 + 4x231
After regex: [('3', '145.80'), ('4', '231')]
Result: 1361.40
Assuming an input such as 1*[VALUE] is fine by you, I believe the following will catch all numeric statements:
(\d[x*] ?)?\d+([.,]\d+)?(?![*x])
Here's the breakdown:
(\d[x*] ?)?
Catches optional multipliers
\d+([.,]\d+)?
Requires the numeric value, with an optional decimal value
(?![*x])
Is a negative lookup to invalidate standalone multipliers as accepted values (e.g. match 1x as the value 1).
Hope I didn't miss anything.
Why not simplify it and have a regex to capture the part trailing last /
(?<=\/)\s[\dx\*\+\.\,\s]+
It would give all the numeric parts and then you have to evaluate the expression.

Simple Regex not working in Perl

I have a simple Perl regex that should match a space between two characters and replace the space with a *. It's simply not working in some cases. The Perl line is this:
s/([A-Za-z0-9])\s+([A-Za-z0-9])/\1 * \2/g;
For example see below: (~> is my zsh prompt)
~> cat mwe
s t Subscript[r, 1]
~> perl -pe "s/([A-Za-z0-9])\s+([A-Za-z0-9])/\1 * \2/g;" < mwe
s * t Subscript[r, 1]
t Subscript[r, 1] isn't being matched. This is just an example. My file is much longer, and while the regex catches most correctly, I can't find a pattern to the ones it doesn't match (and should).
Vim seems to find everything correctly (after the appropriate regex syntax changes).
How can I go about solving this? How can I help diagnose the problem?
Thank you.
Use lookahead instead:
perl -pe 's/([a-z0-9])\s+(?=[a-z0-9])/\1 * /ig' mwe
Output:
s-E^(t * Subscript[r, 1]) t * v-E^(t * Subscript[r, 1]) y-E^(t *
Subscript[r, 1]) t * y+E^t * s * Subscript[r, 1]+2 * E^(t *
Subscript[r, 1]) s * Subscript[r, 1]-3 * E^(t+t * Subscript[r, 1]) s *
Subscript[r, 1]+E^(t * Subscript[r, 1]) s * t * Subscript[r, 1]
Problem is that in your regex you're matching not looking ahead. So for the case of:
perl -pe 's/([a-z0-9])\s+([a-z0-9])/\1 * \2/ig' <<< "a b c"
You will get:
a * b c
Since b has already been matched previously and internal pointer has moved ahead.