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?
-
2Have you tried anything by yourself? – Igl3 Jan 19 '18 at 11:06
-
Show us how the end result should look like! – Srdjan M. Jan 19 '18 at 11:23
-
Taxi:[(h19){h12}] in this case i just want taxi .. that is vehicle names and not the hour information. i have tested few things but nothing is working. – GIS_wonders Jan 19 '18 at 12:15
3 Answers
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.
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.

- 2,482
- 1
- 17
- 37
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
)

- 154,723
- 16
- 55
- 70