Extracting Numeric part with thousands separator and decimal point using regex in c# in a alpha numeric...
I am trying to extract only the number part including the thousands separator and the decimal value , but always getting the error input string not in correct format .
My input string will be in the following formats
USD1200 1,200.12 USD
I need the output as below respectively
1200 1,200.12
If I use the below the first occurrence of , is considered as delimiter
string prodValue = Regex.Match(prodAmount, @"d+").Value;
If I use this one , I am getting error
string prodValue = Regex.Match(prodAmount, @"^[0-9]+(,[0-9])+(.[0-9])?$").Value;
How can I can extract the output I needed?
c# regex
add a comment |
I am trying to extract only the number part including the thousands separator and the decimal value , but always getting the error input string not in correct format .
My input string will be in the following formats
USD1200 1,200.12 USD
I need the output as below respectively
1200 1,200.12
If I use the below the first occurrence of , is considered as delimiter
string prodValue = Regex.Match(prodAmount, @"d+").Value;
If I use this one , I am getting error
string prodValue = Regex.Match(prodAmount, @"^[0-9]+(,[0-9])+(.[0-9])?$").Value;
How can I can extract the output I needed?
c# regex
So you just need to RegEx replace anything that isn't 0-9, "," or "." ?
– Nathan Champion
Nov 24 '18 at 6:40
TryRegex.Replace(s, @"[^0-9.,]+", "").Trim()
– Wiktor Stribiżew
Nov 24 '18 at 12:02
add a comment |
I am trying to extract only the number part including the thousands separator and the decimal value , but always getting the error input string not in correct format .
My input string will be in the following formats
USD1200 1,200.12 USD
I need the output as below respectively
1200 1,200.12
If I use the below the first occurrence of , is considered as delimiter
string prodValue = Regex.Match(prodAmount, @"d+").Value;
If I use this one , I am getting error
string prodValue = Regex.Match(prodAmount, @"^[0-9]+(,[0-9])+(.[0-9])?$").Value;
How can I can extract the output I needed?
c# regex
I am trying to extract only the number part including the thousands separator and the decimal value , but always getting the error input string not in correct format .
My input string will be in the following formats
USD1200 1,200.12 USD
I need the output as below respectively
1200 1,200.12
If I use the below the first occurrence of , is considered as delimiter
string prodValue = Regex.Match(prodAmount, @"d+").Value;
If I use this one , I am getting error
string prodValue = Regex.Match(prodAmount, @"^[0-9]+(,[0-9])+(.[0-9])?$").Value;
How can I can extract the output I needed?
c# regex
c# regex
edited Nov 24 '18 at 7:37
Maksym Labutin
369313
369313
asked Nov 24 '18 at 6:37
pragathi npragathi n
1
1
So you just need to RegEx replace anything that isn't 0-9, "," or "." ?
– Nathan Champion
Nov 24 '18 at 6:40
TryRegex.Replace(s, @"[^0-9.,]+", "").Trim()
– Wiktor Stribiżew
Nov 24 '18 at 12:02
add a comment |
So you just need to RegEx replace anything that isn't 0-9, "," or "." ?
– Nathan Champion
Nov 24 '18 at 6:40
TryRegex.Replace(s, @"[^0-9.,]+", "").Trim()
– Wiktor Stribiżew
Nov 24 '18 at 12:02
So you just need to RegEx replace anything that isn't 0-9, "," or "." ?
– Nathan Champion
Nov 24 '18 at 6:40
So you just need to RegEx replace anything that isn't 0-9, "," or "." ?
– Nathan Champion
Nov 24 '18 at 6:40
Try
Regex.Replace(s, @"[^0-9.,]+", "").Trim()
– Wiktor Stribiżew
Nov 24 '18 at 12:02
Try
Regex.Replace(s, @"[^0-9.,]+", "").Trim()
– Wiktor Stribiżew
Nov 24 '18 at 12:02
add a comment |
4 Answers
4
active
oldest
votes
For capturing multiple matches, you need Regex.Matches
instead of Regex.Match
. Also in your regex, you need to make comma group as zero or more occurence and dot group as optional as it may or may not exist in the number you intend to capture.
You can use this regex,
d+(?:,d+)*(?:.d+)?
Explanation:
d+
--> Matches one or more digits
(?:,d+)*
--> Matches a comma followed by one or more digits zero or more times
(?:.d+)?
--> Optionally matches a literal dot followed by one or more digits
Demo
Here is a sample C# code,
MatchCollection matches = Regex.Matches("USD1200 1,200.12 1,211,234,332.12 12,333 USD", @"d+(?:,d+)*(?:.d+)?");
for (int i = 0; i < matches.Count;i++) {
Console.WriteLine("Match: " + matches[i]);
}
This gives following output like you expect,
Match: 1200
Match: 1,200.12
Match: 1,211,234,332.12
Match: 12,333
add a comment |
I think you places the second "+" wrong and missed a third one.
@"^[0-9]+,[0-9]+.[0-9]+?$")
add a comment |
You Regex
match string is incorrect.
Try: ^[0-9]+((,[0-9]+)+)?(.[0-9]+)?$
See example here
You forgot to escape dot due to which it matches 10%2 or 10P2. Also you don't need to escape comma. With your usage of ^ and $ this regex will not be able to find multiple matches the way OP wants.
– Pushpesh Kumar Rajwanshi
Nov 24 '18 at 9:04
Thanks for pointing out the error. It seems I was in haste and removed the prefixing from the wrong character. It's fixed now.
– Julo
Nov 24 '18 at 9:15
add a comment |
In your regex you use anchors ^
to assert the start and the end $
of the string where USD
is not taken into consideration and would not match. If you want mulitple matches, you should use Regex.Matches instead.
To match a number without a dot or a comma, the middle part should match 0+ times as the last part is already optional and the digits in the character class need an quantifier to match 1+ times like (,[0-9]+)*
. Note that you don't have to escape the comma.
If the USD
is part of the match, you could use an alternation to match either USD1200 or 1,200.12 USD instead of matching all the digits.
You could use named capturing group to reference the group by name.
bUSD(?<n1>d+)|(?<n2>d{1,3}(?:,d{3})*(?:.d+)?) USDb
That would match
bUSD
Match word boundary and USD
(?<n1>d+)
Named capturing groupn1
which will match 1+ digits
|
or
(?<n2>
Start named capturing groupn2
d{1,3}
Match 1-3 digits
(?:,d{3})*
Match a comma, 3 digits and repeat 0+ times
(?:.d+)?
Match a dot and 1+ digits and make that optional
)
Close namedd capturing group
USDb
Match space, USD and a word boundary
Regex demo | C# demo
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455799%2fextracting-numeric-part-with-thousands-separator-and-decimal-point-using-regex-i%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
For capturing multiple matches, you need Regex.Matches
instead of Regex.Match
. Also in your regex, you need to make comma group as zero or more occurence and dot group as optional as it may or may not exist in the number you intend to capture.
You can use this regex,
d+(?:,d+)*(?:.d+)?
Explanation:
d+
--> Matches one or more digits
(?:,d+)*
--> Matches a comma followed by one or more digits zero or more times
(?:.d+)?
--> Optionally matches a literal dot followed by one or more digits
Demo
Here is a sample C# code,
MatchCollection matches = Regex.Matches("USD1200 1,200.12 1,211,234,332.12 12,333 USD", @"d+(?:,d+)*(?:.d+)?");
for (int i = 0; i < matches.Count;i++) {
Console.WriteLine("Match: " + matches[i]);
}
This gives following output like you expect,
Match: 1200
Match: 1,200.12
Match: 1,211,234,332.12
Match: 12,333
add a comment |
For capturing multiple matches, you need Regex.Matches
instead of Regex.Match
. Also in your regex, you need to make comma group as zero or more occurence and dot group as optional as it may or may not exist in the number you intend to capture.
You can use this regex,
d+(?:,d+)*(?:.d+)?
Explanation:
d+
--> Matches one or more digits
(?:,d+)*
--> Matches a comma followed by one or more digits zero or more times
(?:.d+)?
--> Optionally matches a literal dot followed by one or more digits
Demo
Here is a sample C# code,
MatchCollection matches = Regex.Matches("USD1200 1,200.12 1,211,234,332.12 12,333 USD", @"d+(?:,d+)*(?:.d+)?");
for (int i = 0; i < matches.Count;i++) {
Console.WriteLine("Match: " + matches[i]);
}
This gives following output like you expect,
Match: 1200
Match: 1,200.12
Match: 1,211,234,332.12
Match: 12,333
add a comment |
For capturing multiple matches, you need Regex.Matches
instead of Regex.Match
. Also in your regex, you need to make comma group as zero or more occurence and dot group as optional as it may or may not exist in the number you intend to capture.
You can use this regex,
d+(?:,d+)*(?:.d+)?
Explanation:
d+
--> Matches one or more digits
(?:,d+)*
--> Matches a comma followed by one or more digits zero or more times
(?:.d+)?
--> Optionally matches a literal dot followed by one or more digits
Demo
Here is a sample C# code,
MatchCollection matches = Regex.Matches("USD1200 1,200.12 1,211,234,332.12 12,333 USD", @"d+(?:,d+)*(?:.d+)?");
for (int i = 0; i < matches.Count;i++) {
Console.WriteLine("Match: " + matches[i]);
}
This gives following output like you expect,
Match: 1200
Match: 1,200.12
Match: 1,211,234,332.12
Match: 12,333
For capturing multiple matches, you need Regex.Matches
instead of Regex.Match
. Also in your regex, you need to make comma group as zero or more occurence and dot group as optional as it may or may not exist in the number you intend to capture.
You can use this regex,
d+(?:,d+)*(?:.d+)?
Explanation:
d+
--> Matches one or more digits
(?:,d+)*
--> Matches a comma followed by one or more digits zero or more times
(?:.d+)?
--> Optionally matches a literal dot followed by one or more digits
Demo
Here is a sample C# code,
MatchCollection matches = Regex.Matches("USD1200 1,200.12 1,211,234,332.12 12,333 USD", @"d+(?:,d+)*(?:.d+)?");
for (int i = 0; i < matches.Count;i++) {
Console.WriteLine("Match: " + matches[i]);
}
This gives following output like you expect,
Match: 1200
Match: 1,200.12
Match: 1,211,234,332.12
Match: 12,333
edited Nov 24 '18 at 8:49
answered Nov 24 '18 at 8:43
Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi
8,36021027
8,36021027
add a comment |
add a comment |
I think you places the second "+" wrong and missed a third one.
@"^[0-9]+,[0-9]+.[0-9]+?$")
add a comment |
I think you places the second "+" wrong and missed a third one.
@"^[0-9]+,[0-9]+.[0-9]+?$")
add a comment |
I think you places the second "+" wrong and missed a third one.
@"^[0-9]+,[0-9]+.[0-9]+?$")
I think you places the second "+" wrong and missed a third one.
@"^[0-9]+,[0-9]+.[0-9]+?$")
answered Nov 24 '18 at 6:42
Klaus GütterKlaus Gütter
2,55221321
2,55221321
add a comment |
add a comment |
You Regex
match string is incorrect.
Try: ^[0-9]+((,[0-9]+)+)?(.[0-9]+)?$
See example here
You forgot to escape dot due to which it matches 10%2 or 10P2. Also you don't need to escape comma. With your usage of ^ and $ this regex will not be able to find multiple matches the way OP wants.
– Pushpesh Kumar Rajwanshi
Nov 24 '18 at 9:04
Thanks for pointing out the error. It seems I was in haste and removed the prefixing from the wrong character. It's fixed now.
– Julo
Nov 24 '18 at 9:15
add a comment |
You Regex
match string is incorrect.
Try: ^[0-9]+((,[0-9]+)+)?(.[0-9]+)?$
See example here
You forgot to escape dot due to which it matches 10%2 or 10P2. Also you don't need to escape comma. With your usage of ^ and $ this regex will not be able to find multiple matches the way OP wants.
– Pushpesh Kumar Rajwanshi
Nov 24 '18 at 9:04
Thanks for pointing out the error. It seems I was in haste and removed the prefixing from the wrong character. It's fixed now.
– Julo
Nov 24 '18 at 9:15
add a comment |
You Regex
match string is incorrect.
Try: ^[0-9]+((,[0-9]+)+)?(.[0-9]+)?$
See example here
You Regex
match string is incorrect.
Try: ^[0-9]+((,[0-9]+)+)?(.[0-9]+)?$
See example here
edited Nov 24 '18 at 9:10
answered Nov 24 '18 at 6:49
JuloJulo
8771815
8771815
You forgot to escape dot due to which it matches 10%2 or 10P2. Also you don't need to escape comma. With your usage of ^ and $ this regex will not be able to find multiple matches the way OP wants.
– Pushpesh Kumar Rajwanshi
Nov 24 '18 at 9:04
Thanks for pointing out the error. It seems I was in haste and removed the prefixing from the wrong character. It's fixed now.
– Julo
Nov 24 '18 at 9:15
add a comment |
You forgot to escape dot due to which it matches 10%2 or 10P2. Also you don't need to escape comma. With your usage of ^ and $ this regex will not be able to find multiple matches the way OP wants.
– Pushpesh Kumar Rajwanshi
Nov 24 '18 at 9:04
Thanks for pointing out the error. It seems I was in haste and removed the prefixing from the wrong character. It's fixed now.
– Julo
Nov 24 '18 at 9:15
You forgot to escape dot due to which it matches 10%2 or 10P2. Also you don't need to escape comma. With your usage of ^ and $ this regex will not be able to find multiple matches the way OP wants.
– Pushpesh Kumar Rajwanshi
Nov 24 '18 at 9:04
You forgot to escape dot due to which it matches 10%2 or 10P2. Also you don't need to escape comma. With your usage of ^ and $ this regex will not be able to find multiple matches the way OP wants.
– Pushpesh Kumar Rajwanshi
Nov 24 '18 at 9:04
Thanks for pointing out the error. It seems I was in haste and removed the prefixing from the wrong character. It's fixed now.
– Julo
Nov 24 '18 at 9:15
Thanks for pointing out the error. It seems I was in haste and removed the prefixing from the wrong character. It's fixed now.
– Julo
Nov 24 '18 at 9:15
add a comment |
In your regex you use anchors ^
to assert the start and the end $
of the string where USD
is not taken into consideration and would not match. If you want mulitple matches, you should use Regex.Matches instead.
To match a number without a dot or a comma, the middle part should match 0+ times as the last part is already optional and the digits in the character class need an quantifier to match 1+ times like (,[0-9]+)*
. Note that you don't have to escape the comma.
If the USD
is part of the match, you could use an alternation to match either USD1200 or 1,200.12 USD instead of matching all the digits.
You could use named capturing group to reference the group by name.
bUSD(?<n1>d+)|(?<n2>d{1,3}(?:,d{3})*(?:.d+)?) USDb
That would match
bUSD
Match word boundary and USD
(?<n1>d+)
Named capturing groupn1
which will match 1+ digits
|
or
(?<n2>
Start named capturing groupn2
d{1,3}
Match 1-3 digits
(?:,d{3})*
Match a comma, 3 digits and repeat 0+ times
(?:.d+)?
Match a dot and 1+ digits and make that optional
)
Close namedd capturing group
USDb
Match space, USD and a word boundary
Regex demo | C# demo
add a comment |
In your regex you use anchors ^
to assert the start and the end $
of the string where USD
is not taken into consideration and would not match. If you want mulitple matches, you should use Regex.Matches instead.
To match a number without a dot or a comma, the middle part should match 0+ times as the last part is already optional and the digits in the character class need an quantifier to match 1+ times like (,[0-9]+)*
. Note that you don't have to escape the comma.
If the USD
is part of the match, you could use an alternation to match either USD1200 or 1,200.12 USD instead of matching all the digits.
You could use named capturing group to reference the group by name.
bUSD(?<n1>d+)|(?<n2>d{1,3}(?:,d{3})*(?:.d+)?) USDb
That would match
bUSD
Match word boundary and USD
(?<n1>d+)
Named capturing groupn1
which will match 1+ digits
|
or
(?<n2>
Start named capturing groupn2
d{1,3}
Match 1-3 digits
(?:,d{3})*
Match a comma, 3 digits and repeat 0+ times
(?:.d+)?
Match a dot and 1+ digits and make that optional
)
Close namedd capturing group
USDb
Match space, USD and a word boundary
Regex demo | C# demo
add a comment |
In your regex you use anchors ^
to assert the start and the end $
of the string where USD
is not taken into consideration and would not match. If you want mulitple matches, you should use Regex.Matches instead.
To match a number without a dot or a comma, the middle part should match 0+ times as the last part is already optional and the digits in the character class need an quantifier to match 1+ times like (,[0-9]+)*
. Note that you don't have to escape the comma.
If the USD
is part of the match, you could use an alternation to match either USD1200 or 1,200.12 USD instead of matching all the digits.
You could use named capturing group to reference the group by name.
bUSD(?<n1>d+)|(?<n2>d{1,3}(?:,d{3})*(?:.d+)?) USDb
That would match
bUSD
Match word boundary and USD
(?<n1>d+)
Named capturing groupn1
which will match 1+ digits
|
or
(?<n2>
Start named capturing groupn2
d{1,3}
Match 1-3 digits
(?:,d{3})*
Match a comma, 3 digits and repeat 0+ times
(?:.d+)?
Match a dot and 1+ digits and make that optional
)
Close namedd capturing group
USDb
Match space, USD and a word boundary
Regex demo | C# demo
In your regex you use anchors ^
to assert the start and the end $
of the string where USD
is not taken into consideration and would not match. If you want mulitple matches, you should use Regex.Matches instead.
To match a number without a dot or a comma, the middle part should match 0+ times as the last part is already optional and the digits in the character class need an quantifier to match 1+ times like (,[0-9]+)*
. Note that you don't have to escape the comma.
If the USD
is part of the match, you could use an alternation to match either USD1200 or 1,200.12 USD instead of matching all the digits.
You could use named capturing group to reference the group by name.
bUSD(?<n1>d+)|(?<n2>d{1,3}(?:,d{3})*(?:.d+)?) USDb
That would match
bUSD
Match word boundary and USD
(?<n1>d+)
Named capturing groupn1
which will match 1+ digits
|
or
(?<n2>
Start named capturing groupn2
d{1,3}
Match 1-3 digits
(?:,d{3})*
Match a comma, 3 digits and repeat 0+ times
(?:.d+)?
Match a dot and 1+ digits and make that optional
)
Close namedd capturing group
USDb
Match space, USD and a word boundary
Regex demo | C# demo
answered Nov 24 '18 at 10:29
The fourth birdThe fourth bird
23.3k81427
23.3k81427
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455799%2fextracting-numeric-part-with-thousands-separator-and-decimal-point-using-regex-i%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So you just need to RegEx replace anything that isn't 0-9, "," or "." ?
– Nathan Champion
Nov 24 '18 at 6:40
Try
Regex.Replace(s, @"[^0-9.,]+", "").Trim()
– Wiktor Stribiżew
Nov 24 '18 at 12:02