Click here to Skip to main content
15,169,285 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Currently my regex pattern is "hello"

How can I allow either "hello" or "hello ", ie. there must be either nothing after the word or the character directly after it may only be a space.




I now have
(([."])|([ ]))hello\2|\(hello\)

which matches:
but not x hello

How can I allow anything before?

What I have tried:

Looking up regex and not understanding the patterns
Updated 12-Jun-17 23:57pm

Try this:
\s for one space
? repeat zero or once
[no name] 13-Jun-17 5:24am
I now have
(((?=.+?)((["])|([ .]))|\3)hello\4|\(hello\))|\1(?=([. ]+?))

How could I make it allow nothing or only ("." or " ") after?
Richard MacCutchan 13-Jun-17 6:09am
TRo add to Pete Leow's suggestion: "hello" doesn't match just "hello", and "hello\s?" doesn't just match hello with a space after it. It also matches
"hello there"
And anything else which contains the letters "hello" contiguously in that sequence.
If you want to match exactly "hello" or "hello " then you want:

If you want to work with regexes, then get a copy of Expresso[^] - it's free, and it examines, tests, and generates regular expressions. Damn useful, and I wish I'd written it!
[no name] 13-Jun-17 3:57am
This isn't the pattern I want to settle on, but I have the code:

Private Sub updateCodeSyntaxHighlighting()
Dim keywords As New Regex("\bhello\b|\bthere\b")

Dim selPos As Integer = codeEditorBox.SelectionStart
Dim selPos2 As Integer = codeEditorBox.GetFirstCharIndexOfCurrentLine

If Not codeEditorBox.Text = "" Then
codeEditorBox.Select(0, codeEditorBox.Lines(codeEditorBox.GetLineFromCharIndex(selPos)).Length)

codeEditorBox.SelectionStart = selPos2
codeEditorBox.SelectionColor = codeEditorBox.ForeColor

codeEditorBox.SelectionStart = selPos
End If

For Each keywordMatch As Match In keywords.Matches(codeEditorBox.Text)
codeEditorBox.Select(keywordMatch.Index, keywordMatch.Length)
codeEditorBox.SelectionColor = Color.FromArgb(64, 255, 64)
codeEditorBox.SelectionStart = selPos
codeEditorBox.SelectionColor = codeEditorBox.ForeColor

codeEditorBox.SelectionLength = 0
End Sub

which is called in the TextChanged event.

If I type "xhello" and then put a space after "x", the match isn't found. I can't understand what I'm doing wrong
OriginalGriff 13-Jun-17 4:06am
A quick test with "x hello" and it matches fine.
I'd change the regex to make it more readable:


but either will produce the same results.
What happens when you debug the code? What does it show is happening?
[no name] 13-Jun-17 4:17am
"x hello" works fine, but the issue is when I type "xhello" and put the space in afterwards. It doesn't make "hello" green.
OriginalGriff 13-Jun-17 4:20am
That's probably because you aren't calling the method! What does the debugger show?
[no name] 13-Jun-17 4:49am
There are no errors. That method is called on the textchanged event. When I enter that space, it should run the method, see that there's "x hello", then match the "hello". Thing is, it doesn't.

It works with "x hello" but not "xhello" and then putting the space in
OriginalGriff 13-Jun-17 4:56am
And what does the debugger show?
Have you put a breakpoint at the top of the method and stepped through to find out exactly what is happening, or are you assuming "There are no errors" in compilation means the debugger will not show anything?
[no name] 13-Jun-17 5:31am
Ok I solved the issue, needed:
codeEditorBox.SelectionStart = keywordMatch.Index + keywordMatch.Length
instead of
codeEditorBox.SelectionStart = selPos

As it was assumed the caret was at the end of the line, it wasn't selecting the correct range when it wasn't
It should be "hello\s?", see, for instance VB.Net - Regular Expressions[^].
[no name] 13-Jun-17 4:00am
That allows "hellox"
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre -[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
[no name] 13-Jun-17 5:35am
Using Debuggex I've come up with:
(((?=.+?)((["])|([ .]))|\3)hello\4|\(hello\))(?!(.+?))|\1(?=([. ]+?))

However it's still not perfect, but on the way there.

What should be allowed:

space then hello
hello then space
space then hello then space
anything then hello
hello then anything



This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900