Question

Hi I am new to the regex I am trying to match the pattern for bellow line

T071108452T    42D896D5O 3587

I want to match the pattern till T071108452T 42D896D5 and after this i want the Character "O" to match i tried something like this

 (T)(\d{9})(T)(\d{0,19}\s{0,19}\w{0,19})O

but it contains the "O" already with the \w{0,19} and i want to match "O" as specific character any help will be great thanks .

As for the more values are

T065000090T203 93 5797 9O 4037  
T325170628T0108626004D18O01054
T071108452T    42D896D5O 3587

So i want to match "T"-- then 9 digits then -- "T" and then any combination that is alphanumeric till --"O"

Was it helpful?

Solution

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 until O 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top