473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

find first occurence of multiple characters

I have a filename that I want to extract the 1st set of numbers up to either
a ".", "-", "_" or "~" and make that an int.

Or I guess easier just take all the values that are 0-9 up to the 1st non
number value.

So that if I have a value 449231.xml or 449231~1.xml or 449231-1a.xml, I
want to get the 449231 only and make that an integer.

Can that be done in one statement?

Thanks,

Tom
Sep 19 '08 #1
6 1919
Maybe a substring with a regular expression? I know how to strip
non-numerics using a regular expression but how do I grab the numbers until
I get a non-numeric then stop. I can also do this in a for-loop but I was
looking to see if there was a different way.

I don't have to do this, but was curious if there was a way.

Thanks,

Tom

"tshad" <tf*@dslextreme.comwrote in message
news:Ok**************@TK2MSFTNGP02.phx.gbl...
>I have a filename that I want to extract the 1st set of numbers up to
either a ".", "-", "_" or "~" and make that an int.

Or I guess easier just take all the values that are 0-9 up to the 1st non
number value.

So that if I have a value 449231.xml or 449231~1.xml or 449231-1a.xml, I
want to get the 449231 only and make that an integer.

Can that be done in one statement?

Thanks,

Tom

Sep 19 '08 #2
Take a look at the MatchCollection class. This will return the matches from
the regular expression as a collection, each element of which has a value
(or ToString), length and index, whch is the position in the source where
the matched substring occured. If you construct your regular expression
properly then item 0 (which might be null) is the portion you are after.

"tshad" <tf*@dslextreme.comwrote in message
news:Og*************@TK2MSFTNGP06.phx.gbl...
Maybe a substring with a regular expression? I know how to strip
non-numerics using a regular expression but how do I grab the numbers
until I get a non-numeric then stop. I can also do this in a for-loop but
I was looking to see if there was a different way.

I don't have to do this, but was curious if there was a way.

Thanks,

Tom

"tshad" <tf*@dslextreme.comwrote in message
news:Ok**************@TK2MSFTNGP02.phx.gbl...
>>I have a filename that I want to extract the 1st set of numbers up to
either a ".", "-", "_" or "~" and make that an int.

Or I guess easier just take all the values that are 0-9 up to the 1st non
number value.

So that if I have a value 449231.xml or 449231~1.xml or 449231-1a.xml, I
want to get the 449231 only and make that an integer.

Can that be done in one statement?

Thanks,

Tom

Sep 19 '08 #3
On Sep 18, 7:06*pm, "tshad" <t...@dslextreme.comwrote:
I have a filename that I want to extract the 1st set of numbers up to either
a ".", "-", "_" or "~" and make that an int.

Or I guess easier just take all the values that are 0-9 up to the 1st non
number value.

So that if I have a value 449231.xml or 449231~1.xml or 449231-1a.xml, I
want to get the 449231 only and make that an integer.

Can that be done in one statement?

Thanks,

Tom
Well, if you write a function that does this then from the caller's
perspective it would be one statement.

Public Function ExtractNumber(ByVal text As String) As Integer
Dim start As Integer = -1
For i As Integer = 0 To text.Length
If start = -1 Then
If IsNumeric(a(i)) Then
start = i
End If
ElseIf Not IsNumeric(a(i)) Then
Return Integer.Parse(a.Substring(start, i - start)
End If
Next
If start <-1 Then
Return Integer.Parse(a.Substring(start))
End If
Return 0
End Function

You could use a regular expression as an alternative. The code would
probably be shorter, but not necessarily more readable.
Sep 19 '08 #4
This should be pretty easy using regular expressions. As James Hahn
suggested look at MatchCollection. Download Expresso from UltraPico so that
you can experiment with and test various solutions. I think that the
essence of your regex will be /d+ but the devil will be in the details. If
any filename can be thrown at you there might be cases such as x449231~1.xml
which may or may not meet your criterion. If necessary, there is someway to
say that a match must occur at the beginning of a string, I forget the
syntax offhand.

Good Luck, Bob

"tshad" <tf*@dslextreme.comwrote in message
news:Og*************@TK2MSFTNGP06.phx.gbl...
Maybe a substring with a regular expression? I know how to strip
non-numerics using a regular expression but how do I grab the numbers
until I get a non-numeric then stop. I can also do this in a for-loop but
I was looking to see if there was a different way.

I don't have to do this, but was curious if there was a way.

Thanks,

Tom

"tshad" <tf*@dslextreme.comwrote in message
news:Ok**************@TK2MSFTNGP02.phx.gbl...
>>I have a filename that I want to extract the 1st set of numbers up to
either a ".", "-", "_" or "~" and make that an int.

Or I guess easier just take all the values that are 0-9 up to the 1st non
number value.

So that if I have a value 449231.xml or 449231~1.xml or 449231-1a.xml, I
want to get the 449231 only and make that an integer.

Can that be done in one statement?

Thanks,

Tom


Sep 19 '08 #5
On Sep 19, 8:49*am, "eBob.com" <faken...@totallybogus.comwrote:
This should be pretty easy using regular expressions. *As James Hahn
suggested look at MatchCollection. *Download Expresso from UltraPico sothat
you can experiment with and test various solutions. *I think that the
essence of your regex will be /d+ but the devil will be in the details. *If
any filename can be thrown at you there might be cases such as x449231~1.xml
which may or may not meet your criterion. *If necessary, there is someway to
say that a match must occur at the beginning of a string, I forget the
syntax offhand.
Use the ^ character to anchor the match at the beginning on the input.
Sep 19 '08 #6

"James Hahn" <jh***@yahoo.comwrote in message
news:uo**************@TK2MSFTNGP06.phx.gbl...
Take a look at the MatchCollection class. This will return the matches
from the regular expression as a collection, each element of which has a
value (or ToString), length and index, whch is the position in the source
where the matched substring occured. If you construct your regular
expression properly then item 0 (which might be null) is the portion you
are after.
That was exactly what I did.

But then I ran into another problem.

I am moving this into an SqlParameter, but I can't seem move it directly
into the parameter.Value.

SqlParameter parameter = new SqlParameter("@" +
"PCVOrderID", SqlDbType.Int);

System.Text.RegularExpressions.Match oMatch =
System.Text.RegularExpressions.Regex.Match(Path.Ge tFileName(AppSettings.PCVFileName),
"(\\d*)");

Int32.TryParse(oMatch.Groups[1].Value, out
(int)(parameter.Value));

The error I get is:

a ref or out arguement must be an assignable value.

This is.

I tried using

parameter.Value
(int)parameter.Value

And got the same error.

I can do:

parameter.Value = Convert.ToInt32(tf.SectionNumber);

So it is obviously assignable.

And I can do:

parameter = new SqlParameter("@" + "PCVOrderID",
SqlDbType.Int);

System.Text.RegularExpressions.Match oMatch =
System.Text.RegularExpressions.Regex.Match(Path.Ge tFileName(AppSettings.PCVFileName),
"(\\d*)");

Int32.TryParse(oMatch.Groups[1].Value, out iTemp);
parameter.Value = iTemp;
cmd.Parameters.Add(parameter);

Why can't I put parameter.Value there?

Thanks,

Tom
"tshad" <tf*@dslextreme.comwrote in message
news:Og*************@TK2MSFTNGP06.phx.gbl...
>Maybe a substring with a regular expression? I know how to strip
non-numerics using a regular expression but how do I grab the numbers
until I get a non-numeric then stop. I can also do this in a for-loop
but I was looking to see if there was a different way.

I don't have to do this, but was curious if there was a way.

Thanks,

Tom

"tshad" <tf*@dslextreme.comwrote in message
news:Ok**************@TK2MSFTNGP02.phx.gbl...
>>>I have a filename that I want to extract the 1st set of numbers up to
either a ".", "-", "_" or "~" and make that an int.

Or I guess easier just take all the values that are 0-9 up to the 1st
non number value.

So that if I have a value 449231.xml or 449231~1.xml or 449231-1a.xml, I
want to get the 449231 only and make that an integer.

Can that be done in one statement?

Thanks,

Tom


Sep 19 '08 #7

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

Similar topics

0
by: Armin Wagenknecht | last post by:
Hello, i am modelling a TopicMap, and I have the following problem: I want to use two scopes in the topicmap for one occurence which has to satsify BOTH scopes!! Example: I have the...
1
by: Eshrath Ali Khan | last post by:
Hi, I have a requirement where I am transforming a XML into a html using an XSL. I need to create a object for the each and every occurrence of an element named "tgroup" in the xml. Also I need...
4
by: Yves-Alain NICOLLET | last post by:
I have a script that opens a small window containing a button which when clicked performs a find to the next occurence of a string. It works well when the string is made of only one word or of...
10
by: Sean Berry | last post by:
I need to find the second to last occurence of a "." in a string. Basically I am taking a URL like http://this.is.mydomin.com/path/to/file.txt and want to extract /path/to/file.txt I...
3
by: das | last post by:
Hi all, How can I get a row that has only one occurence in a table? Not through 'distinct' because this gets a single row that might have multiple occurences, I want to get only rows that have...
6
by: Registered User | last post by:
Hi experts, I'm trying to write a program to solve the following exercise: Accept three strings from the user. Find the first occurrence of the first string in the third string. If it is present...
1
by: hairlessOrphan | last post by:
Hi! Is there any way to get the Replace() function in MSSQL to replace only the first occurence of a substring? For example, my Name column has the following data: Bob BobBob And I want to...
14
by: inpuarg | last post by:
I want to find a & character using Regex. But not && How can i manage this in c# Quickfind window ? -------------------------------------------------- ne kadar yaşarsan yaşa sevdiğin kadardır...
2
by: gauravguleria | last post by:
wht's the program which takes two strings using command line arguments and finds the occurence of second string in the first string.The program should return the starting position of first...
4
by: Arthur Dent | last post by:
Hello all, I am trying to figure out how to do something in LINQ, IF I can even do it in LINQ. Online samples and such for LINQ is still a bit on the sketchy side. Here is what I want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.