0

私はひもを持っています。文字列には、スペース、ラテン文字、記号 $ を含めることができます。

例:tert ertert$sdfsdf

ただし、記号 $ は、スペースではなく、文字のみで囲む必要があります。

良い文字列:"teststring$sdfsdf"

不正な文字列:"test $sdfsdf"

悪い文字列"test $ string"

この文字列の正規表現を作成するにはどうすればよいですか?

ありがとうございました

4

1 に答える 1

2

Not sure what language you need this for, but this will work /\s+\$|\$\s+/ to match the "bad" scenario.

The logic is

\s+ //at least one space
\$  //follow by a dollar sign
|  // or
\$ //dollar sign
\s+ followed by at least one space

So, if this were Java it would look like:

myString.matches("\\s+\\$|\\$\\s+")

In Perl, it would be:

if(myString =~ m/\s+\$|\$\s+/){
   //found bad example
}

I recommend asking more specifically for a language example cause these do tend to vary a bit from use to use.

Also, the people on this site tend to feel that you should have tried to solve this on your own before coming here for help. So, if you want to avoid seeing snarky comments about what have you already attempted, you should offer your failed attempts as a sacrifice to the petulant forces that be :). Just asking "how do I do this" does leave the person answering with a quandary as to "where do I begin... what do you already know, where are you actually stuck, etc.".

于 2013-01-20T08:28:48.967 に答える