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

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.

Thanks,

Rix

EDIT:

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


which matches:
.hello.
"hello"
(hello)
but not x hello

How can I allow anything before?

What I have tried:

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

Try this:
hello\s?
\s for one space
? repeat zero or once
 
Share this answer
 
v2
 
Share this answer
 
Comments
[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    
hello[\.\s]
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"
"hello-there"
"why-hello!"
And anything else which contains the letters "hello" contiguously in that sequence.
If you want to match exactly "hello" or "hello " then you want:
^hello\s?$

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!
 
Share this answer
 
Comments
[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
Next

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:

\b(hello|there)\b

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
It should be "hello\s?", see, for instance VB.Net - Regular Expressions[^].
 
Share this answer
 
Comments
[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 - perldoc.perl.org[^]
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.[^]
 
Share this answer
 
Comments
[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:

hello
space then hello
hello then space
space then hello then space
(hello)
"hello"
.hello
hello.
anything then hello
hello then anything

Not:

hellox
xhello
xhellox

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