Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to write a regular expression that skip product id

C#
<span class="Price" id="product-id-453">34.65</span>

C#
<span class="Price" id="product-id-553">343.65</span>


C#
Regex regex = new Regex("<span class="\Price\" id="553">(.*)</span>");


I am having problem in id part of span tag
Posted
Updated 26-Jul-18 15:55pm
v5
Comments
BillWoodruff 9-Dec-15 5:24am    
A similar question was asked and answered here yesterday:

http://www.codeproject.com/Questions/1062436/Regular-Expression-Worst-Situation?arn=44
Member 11698684 9-Dec-15 6:47am    
if id is alphanumeric
<pre lang="C#"><span class="Price" id="product-id-543">(.*)<\/span></pre>

The problem is the hardcoding of the ID "553" which will faild for id "453". You can ignore it by regularizing Id part as well. Try this

<span class="Price" id="\d+">(.*)<\/span>
 
Share this answer
 
Comments
Member 11698684 9-Dec-15 7:09am    
if id is alpha-numeric product-id-543

<span class="Price" id="product-id-543">(.)</span>
You might try
C#
Regex regex = new Regex(@"<span class=""Price"" id=""\d+"">(.*)</span>");
 
Share this answer
 
Comments
Member 11698684 9-Dec-15 7:09am    
if id is alpha-numeric product-id-543

<span class="Price" id="product-id-543">(.)</span>
in addition to what _Asif_ says, change you code as well:
C#
Regex regex = new Regex("<span class="\Price\" id="553">(.*)</span>");
Has backslashes in the wrong place, and won't compile:
C#
Regex regex = new Regex("<span class=\"Price\" id=\"\d{3}\">(.*)</span>");



"f our id is alpha-numeric product-id-543"
If you are going to use regexes, then you need to be able to make at least basic changes to them!
C#
Regex regex = new Regex("<span class=\"Price\" id=\"\.+?\">(.*)</span>");
Should do it.

Get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
v2
Comments
Member 11698684 9-Dec-15 7:09am    
if our id is alpha-numeric product-id-543

<span class="Price" id="product-id-543">(.)</span>
OriginalGriff 9-Dec-15 7:17am    
Answer updated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900