|
-
Apr 11th, 2007, 07:40 AM
#1
Thread Starter
Addicted Member
Regular Expressions Ignoring 1st Match (Resolved)
Im trying to make a regular expression to modify a string like below...
Input: 05-0218 C1-AD-0-YN -01
Output: 05-0218 C1 AD 0 YN 01
So basically i want to remove all hypen's accept the first hypen.
I've got it tooo...
(?>-)+
which evalulates to...
Input: 05-0218 C1-AD-0-YN -01
Output: 05 0218 C1 AD 0 YN 01
I'm just learning Regexp at the moment so forgive my stupidity
Last edited by señorbadger; Apr 11th, 2007 at 12:03 PM.
Reason: Solved
-
Apr 11th, 2007, 10:18 AM
#2
Junior Member
Re: Regular Expressions Ignoring 1st Match
dear you dint mentioned wat coding u r using, but anyhow find here the code in vbscript which works on VB as well.
Input = 05-0218 C1-AD-0-YN -01
VarInput = Trim(VarInput)
VarInd = InStr(VarInput, " ")
If we know the functions to work on it, then its always easy for us to break the work into portions then do it. the second line removes all the spaces at start and end of the input using Trim function, to make sure only the actual inputs remain. Then in 3rd line InStr function searches for first available space withing input and returns the index of space within input. Its why, because output shud be with spaces instead of hyphens except first hyphen. So our input have luckily a space after first hyphen and starting of next hyphens series to whome we need to replace with spaces. so we have to make 2 parts of it to work speratly on them then rejoin them.
VarFristPart = Left(VarInput, VarInd)
VarSecondPart = Right(VarInput, (Len(VarInput) - VarInd))
in above lines, as have discussed earlier InStr function checks for given critera in the string and returns numeric index. now the first space is at index 8 which is the position of space in the string. the first line is taking the 8 characters from the left of string of the input into the variable. and the second line is taking the remaining characters from the right into the second variable. How it works? its easy to understand, we are subtracting the space index value from the total length(len functions calculates the total character count) of the input string, so watever the result is, we are taking that much character from the right of the input string. Hence this method has seperated input into 2 parts. Now c the next step:
VarSecondPart = Replace(VarSecondPart, "-", " ")
This code line is now replacing all the hyphens with spaces into second part.
VarInput = VarFirstPart & VarSecondPart
in this code we have rejoined the both parts which is over required result.
Enjoy it
-
Apr 11th, 2007, 11:57 AM
#3
Thread Starter
Addicted Member
Re: Regular Expressions Ignoring 1st Match
Thanks for the reply, the manipulation is done in Javascript but i was looking for a regular expression pattern rather than using string manipulation. I could use indexOf, substring etc but im trying to make my code more effcient.
Thanks for your help! but i'll just keep researching to see if i can find the solution
-
Apr 11th, 2007, 12:02 PM
#4
Thread Starter
Addicted Member
Re: Regular Expressions Ignoring 1st Match
After careful thought i've realised that this isnt what i need, since the 1st instance of the - may actually need removing, Mod's feel free to delete this thread sorry for wasting time
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|