Q: Regular expression

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
 
M

Mattias Sjögren

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
 
A

Armin Zingler

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
 
H

Herfried K. Wagner [MVP]

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'?
 
D

Dragon

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
 
G

Geoff Jones

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
 
R

Ronchese

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top