regex return conditional group

I spent lot time figuring out a simple regex to return a group (only 1st group).

So the string can be - "No purchase required" or "Purchase of $50.00 worth groceries is required."

I am trying to write a regex which can parse "No" or "50" based on the given string.

This is what I have written.

(?:(No) monthly maintenance|Purchase of \$([\d\.]+ worth groceries)

This works fine but I want my output as 1st group/group 1 only.

Answers


Why not just use /(?:No monthly maintenance|Purchase of $([0-9.]+) worth groceries)/.

The match will fail if it's not in one of those formats, and Group 1 matches '' for the "No monthly maintenance" case, or the number for other case.

If you really need to capture the string No or the number, you might need to get a little more complicated and do something like:

/(?:Purchase of $)?([0-9.]+|No) (?:monthly maintenance|worth groceries)/

Most languages count the matching groups in order, regardless of whether they are in a non-capturing group ((?:...|...)), so forcing the interesting part into the first capturing group might be more trouble than it's worth.

Depending on what language you are using you might want to try using two different precompiled regular expressions and return the matching group of the first match, that way you can easily fit the interesting part into the first group.


I'm not sure you can get the result in group number 1, but you can get both results to appear in the same named group. Here's an example in PowerShell:

$input1 = 'Purchase of $50.00 worth groceries is required'
$input2 = 'No monthly maintenance required'

$re = '(?:(?<xyz>No) monthly maintenance|Purchase of \$(?<xyz>[\d\.]+) worth groceries)'

$match = [regex]::Match($input1, $re)
$match.Groups['xyz']

$match = [regex]::Match($input2, $re)
$match.Groups['xyz']

Which results in the following:

Success  : True
Captures : {50.00}
Index    : 13
Length   : 5
Value    : 50.00

Success  : True
Captures : {No}
Index    : 0
Length   : 2
Value    : No

Not all languages support named groups though. Since PowerShell runs on the .NET Framework, this will work for any .NET language.


Need Your Help

Why does assembleDebug takes much longer then project build and how to optimize it?

android android-studio gradle

I've been dealing with gradle build time in Android Studio recently. I've managed to decrease the build time from about 3 mins to 10 seconds by removing some dependencies, but here's the thing:

Display Different Fields on Sharepoint List depending on selected responses in the previous fields?

forms list sharepoint

Beginner Question here - I have a list in SharePoint and I am asking users to respond by clicking the "Add New Item" to bring up the form.