-1

I'm in great trouble.

I must check if a string fits (matches) another string with RegEx. For example, given the following string:

Apr 2 13:42:32 sandbox izxp[12000]: Received disconnect from 10.11.106.14: 10: disconnected by user

In the editable input field I give the program the following shortened string:

Received disconnect from 10.11.106.14: 10

If it fits the existing string (as you can see above), it is OK. If any part of the new edited string doesn't fit the original string, I must warn the user with a message.

Could you help me solving this question with RegEx? Or another method? I would appreciate it!

Freddiboy
  • 133
  • 10

2 Answers2

1

You must get the original string in a variable, let's call it $original (this is perl). Then you must get the input from the "editable input field", let's call it $input.

Then it is a simple

if ($original=~/$input/)
{
   #Your code for a message to the user here 

}
PinkElephantsOnParade
  • 6,452
  • 12
  • 53
  • 91
0

Your solution would be less regex and more escaping. Assuming you're going to use no regex patterns and just search for the input string literal, you should write your function so that it turns this

Received disconnect from 10.11.106.14: 10

into this

Received disconnect from 10\.11\.106\.14: 10

This can be achieved with many different libraries depending on which language you are using.

That will then allow you to check for a match.

Regular Expressions are more designed for common patterns in strings, rather than finding exact literals.

David B
  • 2,688
  • 18
  • 25
  • Hi David, I use Perl, isn't a way to use a code without libraries? Only RegEx. – Freddiboy Jun 03 '12 at 17:03
  • You cannot manipulate strings with regex. Regex is for finding patterns in strings. – David B Jun 03 '12 at 17:03
  • 3
    @DavidB actually, perl regex is perfectly good at matching arbitrary literal strings, that's what the `\Q` escape is for. `if ($string =~ /\Q$substring/)` works as well as `if (index($string, $substring) != -1)` except that it's extensible with other regex features. – hobbs Jun 03 '12 at 18:23
  • @hobbs Consider myself corrected then. I've never touched Perl myself. – David B Jun 03 '12 at 23:34