Is ?: something special in regex?

Status
Not open for further replies.

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Looking at some of yanfly's regex

/<(?:REGION_BATTLEBACKS|region battlebacks)>/iI don't understand what the ?: is doing.Is there something special about it in the context of a capture group?
 
Last edited by a moderator:

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
935
First Language
English
Primarily Uses
N/A
I'm also interested in knowing. I know that :? Would mean zero or one :, but I'm not sure about ?:
 

Solistra

Veteran
Veteran
Joined
Aug 15, 2012
Messages
593
Reaction score
247
Primarily Uses
?: allows you to create match groups that capture matches without generating back-references. Basically, it's a performance optimization when you don't need access to all of the back-references that your regular expression creates -- just the most recent one.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Thanks, I see how it works.


http://rubular.com/r/OoeCIxDhOm


Though, does that performance optimization also apply when you only have one capture group?
 
Last edited by a moderator:

Solistra

Veteran
Veteran
Joined
Aug 15, 2012
Messages
593
Reaction score
247
Primarily Uses
It certainly can, depending on the string you're matching the regular expression against.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Is there a term for that notation?
 

Solistra

Veteran
Veteran
Joined
Aug 15, 2012
Messages
593
Reaction score
247
Primarily Uses
Honestly, I don't know -- and I don't really see the importance of knowing the term if you know what you're doing with it, really.
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
It certainly can, depending on the string you're matching the regular expression against.
What kind of strings/regex would fit that bill?

I guess I'm not really understanding the point of using ?: because in this context the particular note-tag is only used as a delimiter of some sort; the captured contents are never being used anyways. So it's probably a bad example.

Some may be tempted to think that for some reason this

Code:
(?:VALUE)
is better over just

Code:
VALUE
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
Would be useful if you just want to detect if the string exists. So for things you're setting in notes as a flag but don't have any parameters, or for the purposes of that particular section of script, you don't care what the parameters are.
 

Solistra

Veteran
Veteran
Joined
Aug 15, 2012
Messages
593
Reaction score
247
Primarily Uses
Basically, you wouldn't be using ?: for capturing a single flat value -- the point is to optimize grouping, and you certainly wouldn't need to group something like VALUE. What you're doing with ?: is minimizing the memory footprint of running a regular expression where all of the back-references that would normally be generated are legitimately unnecessary. In the example, Yanfly's regular expression matches REGION_BATTLEBACKS and region battlebacks (including all of those case-insensitive variations). Essentially, that had to be defined as a group in order to match what was needed: a group was required, but all of the back-references generated by using it were not.


Edit: Just saw this.

Would be useful if you just want to detect if the string exists. So for things you're setting in notes as a flag but don't have any parameters, or for the purposes of that particular section of script, you don't care what the parameters are.
Then you could just use string['match this'].
 
Last edited by a moderator:

TheoAllen

Self-proclaimed jack of all trades
Veteran
Joined
Mar 16, 2012
Messages
5,592
Reaction score
6,522
First Language
Indonesian
Primarily Uses
RMVXA
Looking at some of yanfly's regex

/<(?:REGION_BATTLEBACKS|region battlebacks)>/iI don't understand what the ?: is doing.Is there something special about it in the context of a capture group?
That's mean you can write your notetag like this

<ReGiOn BaTtLeBaCkS>Correct me if I'm wrong
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
Yes, because of the /i at the end. That's got nothing to do with the ? at the start.
 

Solistra

Veteran
Veteran
Joined
Aug 15, 2012
Messages
593
Reaction score
247
Primarily Uses
That's mean you can write your notetag like this

<ReGiOn BaTtLeBaCkS>Correct me if I'm wrong
No offense, but you are incorrect. That's what the i flag at the end of the regular expression does -- it allows it to match regardless of case. (The "i" stands for "insensitive," as in "case insensitive.")

Edit: Again, the point of using ?: is to make use of match groups without generating the back-references. It's about performance.
 
Last edited by a moderator:

TheoAllen

Self-proclaimed jack of all trades
Veteran
Joined
Mar 16, 2012
Messages
5,592
Reaction score
6,522
First Language
Indonesian
Primarily Uses
RMVXA
No offense, but you are incorrect. That's what the i flag at the end of the regular expression does -- it allows it to match regardless of case. (The "i" stands for "insensitive," as in "case insensitive.")

Edit: Again, the point of using ?: is to make use of match groups without generating the back-references. It's about performance.
Well, that's something new for me ._.

Thanks anyway :D
 

estriole

Veteran
Veteran
Joined
Jun 27, 2012
Messages
1,309
Reaction score
531
First Language
indonesian
/<(?:REGION_BATTLEBACKS|region battlebacks)>/imeans we can write it as

<region_battlebacks>

or

<region battlebacks>

(one using space one using underscore)

so we could also use it as

/<(?:REGION_BATTLEBACKS|region battlebacks|rb|reg_battleb)>/iand we can use <rb> and <reg_battleb>

i think ?: is like "if either regexp inside the bracket match" it will grab the data.

(but it might also means just plain if and the | is the one making the or statement)

i have no actual knowledge about regexp. but it's based on experience on experimenting on regexp (combining victor, yanfly, tsukihime, mr bubble) to make my own notetags grabber script. i usually use trial and error and see what happen when we put that regexp. and i found above when seeing mr bubble script. in his notetags we can use item, armor, weapon or just i, a, w (for shorter notetags).
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
i think ?: is like "if either regexp inside the bracket match" it will grab the data.
No, it is what solistra has said up there.


The pipe | in the parentheses is the one that says either-or.


There are no if conditions in regex, which is why regex is not suitable for certain applications.
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
Since the original question has been answered ...


This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.
 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,047
Members
137,569
Latest member
Shtelsky
Top