Q: Regular expression

  • Thread starter Thread starter Geoff Jones
  • Start date Start date
G

Geoff Jones

Hi

Can anybody give me some advice on a regular expression?

I want to get two substrings out of a string like:

"This is a test & I hope it works"

i.e. I want the substring before the & and the substring after the &:

"This is a " and " I hope it works"

Can anybody tell me what the regular expression, using Regex, is?

Thanks in advance

Geoff
 
Can anybody tell me what the regular expression, using Regex, is?

Any reason you have to use Regex for this? It would be trivial to do
with just the String class (or VB's InStr, Left, Mid functions).



Mattias
 
Geoff Jones said:
Can anybody give me some advice on a regular expression?


Have a look @ this group for language unrelated questions:
microsoft.public.dotnet.framework


Armin
 
Geoff Jones said:
Can anybody give me some advice on a regular expression?

I want to get two substrings out of a string like:

"This is a test & I hope it works"

i.e. I want the substring before the & and the substring after the &:

"This is a " and " I hope it works"

Can anybody tell me what the regular expression, using Regex, is?

What's the reason why you don't use 'Split'?
 
Hi Jeoff,

If you really want to use regex for this job it will be:

~
Dim str As String = "This is a test & I hope it works"
Dim result() As String = Regex.Split(str, "&")
~

However, if I were you I would use:

~
Dim str As String = "This is a test & I hope it works"
Dim result() As String = str.Split("&"c)
~

Hope it helps

Roman
 
Thanks everyone.

The reason I wanted to use Regular expressions was merely to learn more
about it i.e. I agree that the String class can be used but I wondered how
Regex could be used.

Thanks again

Geoff
 
For that specific case, use:

(\w+\s)+(?=&)|(\w+\s)+



Explanation:

(\w+\s)+ => any word followed by a space, one or more times
(?=&) => conditional, specifying the next character must be a '&'
| => optional


so:
(\w+\s)+(?=&) => any word followed by a space, one or more times,
matched with a '&' character


Use the 'The regulator' to test your expressions: http://regex.osherove.com/

[]s
Cesar











"Geoff Jones" <[email protected]> escreveu na mensagem
Hi

Can anybody give me some advice on a regular expression?

I want to get two substrings out of a string like:

"This is a test & I hope it works"

i.e. I want the substring before the & and the substring after the &:

"This is a " and " I hope it works"

Can anybody tell me what the regular expression, using Regex, is?

Thanks in advance

Geoff
 
Back
Top