-2

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?

4 Answers4

0

Just get rid of the square brackets, and the comma and you're good to go!

HelloWorld{1}
WillMaddicott
  • 512
  • 6
  • 20
0

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.

LukStorms
  • 28,916
  • 5
  • 31
  • 45
0

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

0

Try this pattern:

[a-zA-Z]{1,}

You can test it online

enter image description here

Husam Ebish
  • 4,893
  • 2
  • 22
  • 38