473,503 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regular expression question

I posted this in the c# newsgroup by mistake initially...

I am a newbie to regular expressions and want to extract a number from the
end of a string within an HTML document. The string would have these formats:

image/4567
image/45678
image/456789

I would also want to extract the name if possible from this string too:
"image/4567">name</a>

Thanks.
Jan 28 '07 #1
3 940

"JP" wrote:
I posted this in the c# newsgroup by mistake initially...

I am a newbie to regular expressions and want to extract a number from the
end of a string within an HTML document. The string would have these formats:

image/4567
image/45678
image/456789

I would also want to extract the name if possible from this string too:
"image/4567">name</a>

Thanks.
JP,

I think the regex pattern that you want is something like this:
image/(?<image>\d*)\">(?<name>.*)</a

A really nice free regex editor is called expresso. It has a lot of good
examples to help get started.
http://www.ultrapico.com/Expresso.htm

Also, here's some sample code that might help.

Jason Vermillion

aspx tags....

Search in:<br />
<asp:TextBox ID="txtSearchIn" runat="server" Height="144px"
TextMode="MultiLine" Width="606px">asdf p0jasdf
asd image/4567">nameA</aasdf
asdfas as asdimage/45678">name2</aasdf
09823 vasd aimage/456789">n3ame</aasdf
image/4567">name44</aasdfasdfasd</asp:TextBox><br />
<br />
<asp:Button ID="cmdSearch" runat="server" OnClick="Button1_Click"
Text="Search" /><br />
<br />
Matches:<br />
<br />
<asp:ListBox ID="lstMatches" runat="server" Width="614px">
</asp:ListBox>

protected void Button1_Click(object sender, EventArgs e)
{
Regex regex;
MatchCollection mcl;
string input = "";
string pat = @"image/(?<image>\d*)\"">(?<name>.*)</a";

this.lstMatches.Items.Clear();
input = this.txtSearchIn.Text;

regex = new Regex(pat, RegexOptions.IgnoreCase |
RegexOptions.Compiled);
mcl = regex.Matches(input);

foreach (Match m in mcl)
{
// Match has 3 groups, one for the entire match, 1 for the image
number, and one for the name.
// Just peal off the 2nd and 3rd group.
if (m.Groups.Count == 3) {
lstMatches.Items.Add("image #: " + m.Groups[1].Value + "
name: " + m.Groups[2].Value);
}
/*
// Use this if you want to see all of the groups.
foreach (Group g in m.Groups)
{
lstMatches.Items.Add(m.Value + " " + g.Index.ToString() + ":
" + g.Value);
}
*/
}

}
Jan 28 '07 #2


On Jan 28, 7:35 am, Jason Vermillion
<JasonVermill...@discussions.microsoft.comwrote:
"JP" wrote:
I posted this in the c# newsgroup by mistake initially...
I am a newbie to regular expressions and want to extract a number from the
end of a string within an HTML document. The string would have these formats:
image/4567
image/45678
image/456789
I would also want to extract the name if possible from this string too:
"image/4567">name</a>
Thanks.JP,

I think the regex pattern that you want is something like this:
image/(?<image>\d*)\">(?<name>.*)</a
If you need to extract the string from

image/456789 AND "image/4567">name</a>

then I would add ()? (to tell that ">name</a>" at the end is optional)

In this case the final pattern is

image/(?<image>\d*)(\">(?<name>.*)</a)?

Jan 28 '07 #3
On Jan 28, 10:58 am, "Alexey Smirnov" <alexey.smir...@gmail.com>
wrote:
If you need to extract the string from

image/456789 AND "image/4567">name</a>

then I would add ()? (to tell that ">name</a>" at the end is optional)

In this case the final pattern is

image/(?<image>\d*)(\">(?<name>.*)</a)?

....and it could be more simple

(?<=image\/)\d*

Jan 28 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9721
by: Vibha Tripathi | last post by:
Hi Folks, I put a Regular Expression question on this list a couple days ago. I would like to rephrase my question as below: In the Python re.sub(regex, replacement, subject)...
5
2486
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
10
2996
by: Lee Kuhn | last post by:
I am trying the create a regular expression that will essentially match characters in the middle of a fixed-length string. The string may be any characters, but will always be the same length. In...
18
3013
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
5
3088
by: Ryan | last post by:
HELLO I am using the following MICROSOFT SUGGESTED (somewhere on msdn) regular expression to validate email addresses however I understand that the RFP allows for "+" symbols in the email address...
7
371
by: norton | last post by:
Hello, Does any one know how to extact the following text into 4 different groups(namely Date, Artist, Album and Quality)? - Artist - Album Artist - Album - Artist - Album - Artist -...
7
3794
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
6
2271
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
3
2550
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
25
5128
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.