how to represent "anything" using regular expression in VB.net?

tommy_cs

Member
Joined
Jun 7, 2006
Messages
18
Programming Experience
3-5
How should I write a regular expression if I want to get a paragraph start from ABC, follow by anything includes newline, space, tab, any characters and end with DEF?

I try to do it as follow but it match nothing.

System.Text.RegularExpressions.Regex.Match(input, "ABC[\s\w\d]*DEF").Groups(0).ToString:confused:


thanks a lot.
 
I have try in many other way but still match nothing.:(

is it the problem of the newline character in VB.NET? can \s match newline character in vb.net?
if it doesn't work, then I must read the whole text line by line in the code...maybe it is the limitation of VB.NET syntax :(
 
Maybe you should try with singleline option? regex.match(input, pattern, option)
Documentation says it will include linefeed in '.' (any) search, operates as if the multiline text was a long string I think.
 
Thanks a lot...
o..I made a mistake..the white space escape sequence "\s" can actually be used to match newline..like this "ABC\sDEF":)

finally solved the problem..
 
How should I write a regular expression if I want to get a paragraph start from ABC, follow by anything includes newline, space, tab, any characters and end with DEF?

I try to do it as follow but it match nothing.

System.Text.RegularExpressions.Regex.Match(input, "ABC[\s\w\d]*DEF").Groups(0).ToString:confused:


thanks a lot.

OK, you cant use Groups(0) because your regular expression doesnt have any capturing groups, other than the universal group ,which is by default, the entire input

You can use Captures, which are not related to capturing groups, but the other problem you will face is that your RegEx pattern is mangled.


You need to have a pattern of:

ABC.*?DEF

By default, your pattern is too greedy, and for an input of:

ABC
paragraph
DEF
ABC
paragraph
DEF

it will match the entire thing.

Your regexoptions need to be set to SingleLine. Without this option set, your input text will be split into lines and your regex applied to each line. Then, your pattern needs to be set to pessimistic, rather than greedy, with the .*?, rather than .* (or yours, as it stands, is [\s\w\d]* <- star is greedy. starquestion is pessimistic)

I'll polish up a util i use to write regexes, and attach it... GImme a few minutes! :D
 
I see..I need to be more careful
I am new to regular expression..
sometimes the result is not as I expected:D

is the default Option "RegexOptions.Multiline"?
 
Last edited:
OK, here's the util.. It should be quite simple to use, but if you get stuck, do ask!
 

Attachments

  • Release.zip
    7.4 KB · Views: 23
Back
Top