473,800 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1939
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******** ******@TK2MSFTN GP02.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******** *****@TK2MSFTNG P06.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******** ******@TK2MSFTN GP02.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...@dslextrem e.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(B yVal 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(star t, i - start)
End If
Next
If start <-1 Then
Return Integer.Parse(a .Substring(star t))
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******** *****@TK2MSFTNG P06.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******** ******@TK2MSFTN GP02.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...@total lybogus.comwrot e:
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.co mwrote in message
news:uo******** ******@TK2MSFTN GP06.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.Reg ularExpressions .Match oMatch =
System.Text.Reg ularExpressions .Regex.Match(Pa th.GetFileName( AppSettings.PCV FileName),
"(\\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.SectionNumb er);

So it is obviously assignable.

And I can do:

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

System.Text.Reg ularExpressions .Match oMatch =
System.Text.Reg ularExpressions .Regex.Match(Pa th.GetFileName( AppSettings.PCV FileName),
"(\\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******** *****@TK2MSFTNG P06.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******* *******@TK2MSFT NGP02.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
1188
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 textdocument "behaviour in school" which is only valable
1
2596
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 to assign a unique id for this object based upon the its occurence in the xml document. (note: the unique id should not be assigned depeneding upon its position with respect to the parent element but with respect to its number of occurence in the...
4
1439
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 consecutive words. Now I want it to find the next occurence of one of the words contained in a list, ie. I want to dynamically change the string to find. I failed to use a regular expression instead of a string in the find statement. Any idea of...
10
5316
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 thought I would be able to do it like this: ----------------------------------------------------
3
9293
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 only one occurence in the table. I tried the group by and within the having clause I placed the count(myfield) = 1, did not work...
6
5803
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 replace the second string in its place. Note: First and Second string might not be of the same length. E.g.: First string: "cat" Second string: "camel" Third string: "concatenate"
1
11923
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 replace "Bob" with "Robert," but *only* the first occurence: Robert
14
2048
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 ömrün :( --------------------------------------------------
2
2416
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 occurence if it is available otherwise should display "NOT FOUND".
4
2648
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 do... I have some string, whatever, that doesn't matter. I want to find all the characters which occur in the string more than once.
0
9690
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10501
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10273
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10250
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7574
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5469
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
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 we have to send another system
3
2944
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.