---
title: "How to find all strings except one string using regex"  
description: "How to find all strings except one string using regex"  
author: "Revati S Misra"  
published: 2023-08-29  
updated: 2023-09-04  
canonical: https://www.mindstick.com/forum/159715/how-to-find-all-strings-except-one-string-using-regex  
category: "c#"  
tags: ["regex", "string", "regular expression"]  
reading_time: 2 minutes  

---

# How to find all strings except one string using regex

How to find all [strings](https://www.mindstick.com/forum/161912/explain-the-python-strings) [except one](https://answers.mindstick.com/qa/34842/the-economy-of-redlands-has-inspired-some-famous-nicknames-for-the-city-it-has-been-known-by-each-of-the-following-names-in-its-history-except-one-which-one) [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) using [regex](https://www.mindstick.com/forum/160352/how-to-use-regex-in-javascript-for-pattern-matching)

## Replies

### Reply by Aryan Kumar

Sure, there are a few ways to find all the strings [except](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) one string using regex. Here are a few methods:

- **Use the `^` and `<span class="math-inline">\` metacharacters.** The `^` metacharacter matches the beginning of the string, and the `` metacharacter matches the end of the string. So, to find all the strings except the string "world", you would use the following regex pattern:

```plaintext
^(.*)world$
```

The `.` metacharacter matches any character, including whitespace.

- **Use the** `negative lookahead` **construct.** The negative lookahead construct allows you to match a pattern only if it is not followed by another pattern. So, to find all the strings except the string "world", you could also use the following regex pattern:

```plaintext
(.*?)[^world]
```

The `[^world]` part of the pattern matches any character except "world".

- **Use the** `preg_match_all()` **function.** The `preg_match_all()` function in PHP is a regular expression function that can be used to get all of the matches of the pattern in the string. The syntax for the `preg_match_all()` function is as follows:

PHP

```plaintext
preg_match_all(pattern, string, matches)
```

Where:

- `pattern` is the regular expression pattern that you want to match.
- `string` is the string that you want to search.
- `matches` is an array that will contain the results of the match.

The `preg_match_all()` function will return an array of all of the matches of the pattern in the string.

Here is an example of how to use this function:

PHP

```plaintext
$string = "hello world hello world";
$pattern = "(.*?)[^world]";

$results = preg_match_all($pattern, $string);

foreach ($results as $match) {
 echo $match[0];
}
```

This code will output the following:

```plaintext
hello
hello
```

The `match[0]` element of the results array contains the text that was matched by the pattern.


---

Original Source: https://www.mindstick.com/forum/159715/how-to-find-all-strings-except-one-string-using-regex

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
