Home > Software engineering >  Regex ignore everything after a character
Regex ignore everything after a character

Time:11-25

I have following regex.

^(.*[^0-9])([0-9A-Fa-f]{8}[-]?[0-9A-Fa-f]{4}[-]?[0-9A-Fa-f]{4}[-]?[0-9A-Fa-f]{4}[-]?[0-9A-Fa-f]{12})(.*)$

It splits a given text into 3 groups. 1:Pre-GUID, 2:GUID and 3:post-GUID text.

Input: /user/questions/9c8a8823-d88c-4402-a2c1-4530a966f993/help

Results:
Group 1: /user/questions/
Group 2: 9c8a8823-d88c-4402-a2c1-4530a966f993
Group 3: /help

However, I have some instances where GUID is followed by a special character such as @ and in that case I want to ignore everything after GUID ignored i.e. 3rd group that is post GUID be empty.

Input: /user/questions/9c8a8823-d88c-4402-a2c1-4530a966f993@help

Results:
Group 1: /user/questions/
Group 2: 9c8a8823-d88c-4402-a2c1-4530a966f993
Group 3: 

In other terms i don't want regex to consider anything if it encounters a @.

CodePudding user response:

If I understand you correctly, if the last part of the string is :<whatever> or @<whatever> the group 3 should be empty:

^(.*[^0-9])([0-9A-Fa-f]{8}[-]?[0-9A-Fa-f]{4}[-]?[0-9A-Fa-f]{4}[-]?[0-9A-Fa-f]{4}[-]?[0-9A-Fa-f]{12})([^:@].*$|)

Regex demo.


Only the last group is changed to ([^:@].*$|) - match any character but ^/@ to end of string or match empty string.

CodePudding user response:

If you want the third group to be none, you may replace it with:

([^@:].*$)?

If you want the third group to be empty, you may use:

([^@:].*$|)

This will look for either @ or :. You may add more characters to the negated character class as needed.

Demo.


There's one more improvement that I feel inclined to recommend though. Currently, your pattern will match GUID's that have hyphens in some places but not others. To fix this, we can add the first hyphen in a capturing group and replace the subsequent ones with a backreference:

^(.*[^0-9])([0-9A-Fa-f]{8}(-?)[0-9A-Fa-f]{4}\3[0-9A-Fa-f]{4}\3[0-9A-Fa-f]{4}\3[0-9A-Fa-f]{12})([^@:].*$)?

Demo.

Note that in this case, the last part will be in group 4 instead of group 3.

  • Related