Any regex wizards here?
How come this expression:(?:")?(\S+)(?:")?
Given this text:"dicks" dicks "dicks
Results in:MATCH 1
1. [1-7] `dicks"`
MATCH 2
1. [8-13] `dicks`
MATCH 3
1. [15-20] `dicks`
As you can see, the first match has a trailing " even though the trailing " is in non-capturing group, what's weirder is that it works fine for the leading ".
>>54371705
[a-zA-Z]+?(\S+)(?:")?
>>54371731
Won't work and it also eats the first character. What I'm trying to achieve is that I want to capture words but the quotation marks are optional, they can be either at the end of the word, at the beginning at the word, both, or not there at all.
>>54371705
regexr.com
>>54371762
Oh, gotcha. I thought you were just trying to capture just the text.
>>54371705(?:")?
&(\S+)(?:")?
are still trying to(?:")
Which is why "dicks(") & "dicks() return dicks(").
>>54371938
Ohh I actually get it, the (\S+) is matching the trailing " so the last (?:")? is getting ignored, that makes sense, now just need to figure out a way to exclude that ".
>>54372041(?:")?(\S+?)(?:")?
If your language's regex engine supports it, that is.
>>54372131
>>54372041
Wait, no, that's wrong.
It would really help to know what you're trying to achieve, but maybe this would be better:(?:")?([^\s"]+)(?:")?
not helping you with your homework, you week 9 aussie faggot
>>54372176
OOOOOOOOOOOOOOOOOOOOOOHHH SHIT SON CALLED THE FUCK OUT
>>54372147
That's perfect! Works as expected, thanks mate. (What I was trying to achieve was >>54371762).
>>54372176
Not homework, writing some sort of a simple bulletin board, adding support for basic bbcodes using re.sub with Python.
>>54372147
Alternatively, you can just list all 4 of your cases:("\S+"|"\S+|\S+"|\S+)
But the problem with this is you'd include quotes into capturing group.
You can create 4 capturing groups:(?:"(\S+)"|"(\S+)|(\S+)"|(\S+))
The problem with this is you'd have to handle 4 groups in code.
Finally, there is "branch reset" pattern:(?|"(\S+)"|"(\S+)|(\S+)"|(\S+))
It creates just one group, but not all regex implementations support it.
The problem with my solution in >>54372147 is that if you want to allow quote characters inside words (for whatever reason), you can't.
>>54372041
If it is something you think others might have automated before, this website
commandlinefu.com
might already have it figured out for you.
Good luck.
>>54372305
also, is there really a use for putting one possible match into a non-capture group?
>(?:")
(?:) just means "don't capture this", it has nothing to do with making anything optional, you only need ()? to make a group optional(ly match)
>>54371762\"?[A-Za-z]+\"?
>>54373446
To capture apostrophe's as well (eg "you're"):\"?[A-Za-z\']+\"?