Say you have the following string:
T071108452T 42D896D5O 3587O
Note that I added an extra O --^
If you want to match until the last O
, you may use the following pattern: [\w\s]+(?=O)
This means:
[\w\s]+
match words and whitespaces one or more times, greedy.(?=O)
Zerowidth lookahead assertion to match untilO
found
Now if you want to match until the first occurence of O
then you may use the following pattern: [\w\s]+?(?=O)
. Note the added question mark, it's to match ungreedy.
Note: \w
will also match an underscore, you may replace [\w\s]
by [^\W\S_]
to prevent that. Note the negation and the uppercased letters.