TtT23
TtT23

Reputation: 7030

Retrieving (Validating) a portion of a string

Given a string, I need to verify that the string contains the word $LOOP strictly. For instance,

$LOOP(0,"0000","00000009","00000010",5000,100,0,0,0,0,10,0,0);

would return as true while

$LOOP_A("asdf","zxcv")
$LOOP_INIT(0);

would return as false.

The problem I'm having with string.Contains($LOOP) is that it also returns true for the below two strings.

How can I solve this problem?

Edit 1:

Here are some other examples:

$LOOPING should return false
$LOOP$LOOP should return false
$LOOP(anything here) should return true
$LOOP        (Anything here) should return true
$asdLOOP should return false

Upvotes: 1

Views: 64

Answers (3)

Antony Thomas
Antony Thomas

Reputation: 3686

What you are looking for is perhaps REGEX with Word boundary. This will do the trick

Regex re = new Regex(@"\$LOOP\b");

Upvotes: 2

dotNET
dotNET

Reputation: 35400

Assuming that this is C# and .NET, you can use RegEx for your purpose:

bool Res = System.Text.RegularExpressions.Regex.IsMatch(YourString, @"^.*\$LOOP\b.*$");

However, I'd say that your question is a bit ambiguous. Exactly which characters do you want to allow after the string "$LOOP"? For example, should it match "$LOOPING" or "$LOOP$LOOP" or "$LOOP (something)"? Once you decide that, the above expression can be improved.

Upvotes: 0

Superbest
Superbest

Reputation: 26592

Perhaps you can just use the pattern \$LOOP\(.*\);?

\$LOOP[^_\w] will match $LOOP not followed by _ or a word character.

Alternatively, you can split the pattern at $LOOP and see if any of the resulting fragments start with a character that is not part of your words (for example, ( but not _)

Upvotes: 3

Related Questions