Currently i using this pattern: [HelloWorld]{1,}
.
So if my input is: Hello -> It will be match.
But if my input is WorldHello -> Still match but not right.
So how to make input string must match exactly will value inside pattern?
-
1What exactly is your requirement? – yeshashah Jul 21 '18 at 10:36
4 Answers
Just get rid of the square brackets, and the comma and you're good to go!
HelloWorld{1}

- 512
- 6
- 20
In regex what's between square brackets is a character set.
So [HelloWorld]
matches 1 character that's in the set [edlorHW]
.
And .{1,}
or .+
both match 1 or more characters.
What you probably want is the literal word.
So the regex would simple be "HelloWorld"
.
That would match HelloWord in the string "blaHelloWorldbla".
If you want the word to be a single word, and not part of a word?
Then you could use wordboundaries \b
, which indicate the transition between a word character (\w = [A-Za-z0-9_]
) and a non-word character (\W = [^A-Za-z0-9_]
) or the beginning of a line ^
or the end of a line $
.
For example @"\bHelloWorld\b"
to get a match from "bla HelloWorld bla" but not from "blaHelloWorldbla".
Note that the regex string this time was proceeded by @
.
Because by using a verbatim string the backslashes don't have to be backslashed.

- 28,916
- 5
- 31
- 45
it seems you need to use online regex tester web sites to check your pattern. for example you could find one of them here and also you could study c# regex reference here

- 88
- 1
- 7