I am using following lines of code
<asp:TemplateField HeaderText="Phone" SortExpression="Phone">
<ItemTemplate>
<asp:Label ID="lblPhone" runat="server">
<%# Regex.Replace(Eval("Phone").ToString(), #"(\d{3})(\d{3})(\d{4})", "($1)-$2-$3") %>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
It is formatting numbers like 9419002345 but not numbers like 1408-464-1680 or 463237062... Please help me in writing accurate regular expression.
That is because your regular expression just matches when there are ten digits. Your 463237062 has only 9.
You should make the algorithm allow matching on 9 digits too, for example like this:
#"(\d{1,3})(\d{3})(\d{4})"
You can try this:
(\d{10}) will compare 10 numbers
OR
4 digits-3 digits-4 digits.
(\d{10})|(\d{4}-\d{3}-\d{4})
Adjust number of digits as per your need.
Related
I want to replace increment number using regular expression in TextPad. I have below code and I want increment number between tag <EndToEndId> and </EndToEndId> and number should remain 8 digits.
<PmtId>
<EndToEndId>80000001</EndToEndId>
</PmtId>
<PmtTpInf>
<Prtry>PM</Prtry>
</PmtTpInf>
<PmtId>
<PmtId>
<EndToEndId>80000002</EndToEndId>
</PmtId>
<PmtTpInf>
<Prtry>PM</Prtry>
</PmtTpInf>
<PmtId>
......
<PmtId>
<EndToEndId>800000010</EndToEndId>
</PmtId>
<PmtTpInf>
<Prtry>PM</Prtry>
</PmtTpInf>
<PmtId>
I have tried myself to come up with solution but after 80000009, it gives 800000010 which is 9 digit number.
I have provided below regular expression in Find And Replace option in TextPad.
Find What: (<EndToEndId>).*?(</EndToEndId>)
Replace With: (<EndToEndId>)\i(</EndToEndId>)
I have searched similar solution on Stackoverflow using Notepadd++ from Notepad++ incrementally replace
but it doens't give increment number when there are other tags like <PmtId>, <PmtTpInf>
Could you please help me to solve this issue as I have tried myself a lot and now asking on Stackoverflow. Thank you.
Do two passes.
The first pass as you are currently doing, producing results like:
80000001
80000009
800000010
800000099
8000000100
8000000999
then a second pass to correct the lengths:
Search: 80+(\d{6})\b
Replace: 8$1
Which produces the following result from the above sample intermediate output:
8000001
8000009
8000010
8000099
8000100
8000999
I think this is probably easy but I don't have the time to learn how to do it.
In a html file, I have a certain class of paragraph, let´s say:
<p class="footnote"></p>
The "p" tag is always followed by numbers, which increase by one in every instance. Let's say the first number is "43". I want the series of numbers to start from 1, so I need to substract 42 from all paragraphs.
For example, I would want to go from:
<p class="footnote">43. Lorem</p>
<p class="footnote">44. Ipsum</p>.
<p class="footnote">45. Dolor</p>.
to
<p class="footnote">1. Lorem</p>
<p class="footnote">2. Ipsum</p>.
<p class="footnote">3. Dolor</p>.
How can I do it?
If you're looking for a regex that'll handle <p class="footnote">43. Lorem</p> the answer is don't parse HTML with regex.
Assuming you've extracted the string 43. Lorem from a tag and you want to get a number out then it depends on your requirements:
To find any number: \d+
To find any number at the beginning: ^\d+
To find any number followed by a period: \d+\.
A more complete solution will require more details about the problem including the programming language you want to use.
Get the text using javascript (though I'll be using jQuery), split the text, get the first element, and convert to an integer.
$(".footnote").each(function(){
var text = $(this).text(); // Get text
var num = text.split(/\s+/g)[0]; // Split by whitespace and get the first elem
console.log(parseInt(num)); // Convert the elem to an int
});
Is there a way to include a pattern in the search but then not include it in the final output?
I'm trying to find a way to take out just the state in the source code of an address. So my input is
<strong class="street-address">
<address itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
<span itemprop="streetAddress">10937 W Pico Blvd</span><br>
<span itemprop="addressLocality">Los Angeles</span>,
<span itemprop="addressRegion">CA</span>
<span itemprop="postalCode">90064</span>
</address>
</strong>
(the actual source code is much longer for the page) but I want to look up using Regex:
postalCode">[0-9]{5}
and then only take out the snippet of [0-9]{5} instead of the postalCode"> part in the beginning. The issue comes up when I have to search the whole source code as there is inevitably going to be other 5 digit numbers in the entire source code somewhere. Anyway to say, "look for postalCode">" and then take the next 5 digits if they fit the pattern [0-9] for 5 digits?
Use a look behind:
(?<="postalCode">)\d{5}
Look behinds, which have the syntax (?<=...), assert, but do not capture, the input that immediately precedes the matched input. The match returned would be just the 5 digits.
I have a input of type number, and I want to make sure it only accepts a number. I can do this fine on the Server side, but with AngularJS I can not get it to work.
Here's the code:
<input type="number" ng-pattern="/[0-9]+/" name="numOfFruits" ng-model="basket.numOfFruits" />
I suspect this has something to do with the pattern I am supplying [0-9]+ basically I only want numbers in this text box, anything that is not made up of the numbers 0 to 9, I want the input invalid.
At the moment, my input field sees this aa23423 as valid input!
You need to use anchors:
/^[0-9]+$/
^: Start-of-line anchor.
[0-9]+ One or more numbers between 0 and 9.
$: End-of-line anchor.
So this matches the start of the string, then it matches the one or more digits, after that it looks for the end-of-string, so it matches a string containing only numbers and nothing else.
Otherwise, /[0-9]+/ will match only a part of aa23423, more accurately the number 23423 and thus will give you valid.
Here is regexp to validate floating point numbers, both positive and negative:
/^-?[0-9]\d*(\.\d+)?$/
Use this regexp in 'text' input, example:
<input type="text" ng-model="score" ng-pattern="/^-?[0-9]\d*(\.\d+)?$/" required/>
Pattern don't work for input with type="number".
You can use type="text" and than convert value to number
Try defining your regex as a scope variable
In the controller, it worked for me.
regular expression not to allow zero
it should allow 0.0000001 as value but should not allow to enter 0.
I need validator not javascript
I think all you need is this ^(?=.*[1-9])\d*\.?\d*$
But, you could get fancy and allow only a single leading zero if its before a decimal point.
^(?=.*[1-9])(?:[1-9]\d*\.?|0?\.)\d*$
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Input is not valid."
ValidationExpression="^(?=.*[1-9])(?:[1-9]\d*\.?|0?\.)\d*$"></asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
a regexp like that?
([1-9](\.[0-9]+)?)|(0\.[0-9]*[1-9])
looks like working ;-)
if you remove the braces it looks more understandable:
[1-9](\.[0-9]+)? | 0\.[0-9]*[1-9]
If you want to allow both Positive or Negative numbers but not 0:
^(-?.*[1-9])\d*\.?\d*$