3

What is the regular expression for a string that should not contain a pipe (|) character? e.g., "this is an example |" of a string with a pipe character.

Lulu
  • 231
  • 1
  • 4
  • 7

3 Answers3

5

Use a negated character class:

\A[^|]*\z

Explanation:

\A    # Start of string
[^|]* # Match zero or more characters except |
\z    # End of string
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
2

Normally, you don't need a regex to find out if a character is included in a string or not.

You didn't specify a language; e.g. in Perl, you could use the tr operator:

if( $string !~ tr/|// ) {
   ...

or you could just look up the character and check its index (-1 if not there):

if( index($string, '|') == -1 )
   ...

Other languages surely have comparable language constructs (VB.NET, Java, SQL, Matlab, C++ etc.).

Community
  • 1
  • 1
rubber boots
  • 14,924
  • 5
  • 33
  • 44
0

If what you wanted to ask is: what is a regex to match a line that haven't got the pipe symbol then: the answer is: ^[^|]*$

That is match an entire line of zero or more characters that are not |

Chen Levy
  • 15,438
  • 17
  • 74
  • 92