Ankit Goyal
Ankit Goyal

Reputation: 151

regex to extract field

I have one regex to extract the scriptname from a full command. It's working fine for most of the cases, but not working for the first row in sample data.

Sample Data

qcst_out_macroPlayback_13918_14112017-163000.29129.xml
qcst_mxtimingMonitoring.sh -SpecificInputLaunchers
qcst_tps_monitoring.sh -LatestDeals

Output Should be

macroPlayback (not working)
mxtimingMonitoring (working)
monitoring (working)

Regex which is not working for 1st row

\S+_(?<ScriptName>[^\.]+)\.\S+

https://regex101.com/r/cFjn85/1

Upvotes: 1

Views: 5696

Answers (1)

ctwheels
ctwheels

Reputation: 22837

Code

See regex in use here

^\S+_\K[a-z]+

If \K is not available in your flavour of regex, you can use the following instead. This will capture the contents you are looking for into the first capture group.

^\S+_([a-z]+)

Explanation

  • ^ Assert position at start of line
  • \S+ Match any non-whitespace character one or more times
  • _ Match the underscore character _ literally
  • \K Reset the starting point of the reported match. Any previously consumed characters are no longer included in the final match
  • [a-z]+ Match one or more characters in the set a-z (lowercase ASCII alpha characters)

Note: The modifiers gmi are used to make the regex global, multiline and case-insensitive (to match uppercase ASCII alpha characters with [a-z])

Upvotes: 1

Related Questions