-1

I have a string like a Taxi:[(h19){h12}], HeavyTruck :[(h19){h12}] wherein I want to keep information before the ":" that is a taxi or heavy truck . can somebody help me with this?

3 Answers3

0

I think this will do the trick in your case: (?=\s)*\w+(?=\s*:)

Explanation:

(?=\s)* - Searches for 0 or more spaces at the begging of the word without including them in the selection .

\w+ - Selects one or more word characters.

(?=\s*:) - Searches for 0 or more white spaces after the word followed by a column without including them in the selection.

Community
  • 1
  • 1
DobromirM
  • 2,017
  • 2
  • 20
  • 29
0

This will capture a single word if it's followed by :[ allowing spaces before and after :.

[A-Za-z]+(?=\s*:\s*\[)

You'll need to set regex global flag to capture all occurrences.

Justinas Marozas
  • 2,482
  • 1
  • 17
  • 37
0

To match the information in your provided data before the : you could try [A-Za-z]+(?= ?:) which matches upper or lowercase characters one or more times and uses a positive lookahead to assert that what follows is an optional whitespace and a :.

If the pattern after the colon should match, your could try: [A-Za-z]+(?= ?:\[\(h\d+\){h\d+}])

Explanation

  • Match one or more upper or lowercase characters [A-Za-z]+
  • A positive lookahead (?: which asserts that what follows
  • An optional white space ?
  • Is a colon with the pattern after the colon using \d+ to match one or more digits (if you want to match a valid time you could update this with a pattern that matches your time format) :\[\(h\d+\){h\d+}]
  • Close the positive lookahead )
The fourth bird
  • 154,723
  • 16
  • 55
  • 70