-5

Dono what this regular expression is doing

(?>[^\,]*\,){3}([^\,]*)[\']?
(?>[^\,]*\,){4}([^\,]*)[\']?

could any one explain me more in deatil

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Mounarajan
  • 1,357
  • 5
  • 22
  • 43
  • 3
    It's trying to match something. We won't be really helping you by providing an answer for this very specific question. Instead, you should go through a [tutorial](http://www.regular-expressions.info/tutorial.html) to *learn*. – Maroun May 19 '15 at 12:47
  • @MarounMaroun, come on people the user is askling to understand why the downvotes?? – Nikos M. May 19 '15 at 12:48
  • [Google is your friend](http://rick.measham.id.au/paste/explain.pl?regex=%28%3F%3E%5B%5E%5C%2C%5D*%5C%2C%29%7B3%7D%28%5B%5E%5C%2C%5D*%29%5B%5C%27%5D%3F) – whereswalden May 19 '15 at 12:50
  • 1
    @NikosM. The question doesn't demonstrate minimal efforts. I *do* downvote questions like this one. – Maroun May 19 '15 at 12:50
  • take a look [here](http://stackoverflow.com/questions/2973436/regex-lookahead-lookbehind-and-atomic-groups), i have an online [tool here](http://foo123.github.io/examples/regex-analyzer/) which analyses regexes, but does not handle atomic groups and such, but maybe it can be helpful to you – Nikos M. May 19 '15 at 12:50
  • @NikosM. the website get stuck when asking to analyze `(?!a)b` – Maroun May 19 '15 at 12:52
  • @MarounMaroun, sometimes even to demonstrate minimal effort one has to start somewhere and this somewhere needs to be found, so if you dont want to answer you can atleast point to relevant resources and such, dont forget the objectiv e is prociding solutions and answers (even in the form of pointers for further study) – Nikos M. May 19 '15 at 12:53
  • @NikosM. I did (note the link in my comment). – Maroun May 19 '15 at 12:53
  • @MarounMaroun, the anlysis of [`/(?!a)b/`](http://foo123.github.io/examples/regex-analyzer/#action=analyze&regex=%2F%28%3F!a%29b%2F) by my tool (you have to add regex delimiters `/`) – Nikos M. May 19 '15 at 12:55
  • @NikosM.: So we have to guess that your analyzer requires us to add delimiters, and if we don't, the site locks up? That's an interesting design. ;) If delimiters are so important to you, why not add them yourself, like [Rubular](http://rubular.com/) does? – Alan Moore May 19 '15 at 23:33
  • @AlanMoore, the regex can also have flags after the last delimiter so it is nnot good to add them automaticaly at least for the concept of the demo there. As for the guessing if one acceses the regex-analyzer page the first time it already has a sample regex setup as an example so no guesing is needed. This is just a demo no fancy things plus it has the ability to take parameters via url so people can share an analysis through the internet. But maybe i should add a text explaining to add delimiters – Nikos M. May 20 '15 at 01:30
  • @NikosM.: I never even looked at the example--who does? I just selected the whole thing and pasted my own regex in its place. It's not a slam dunk, but making the user supply the delimiters violates the Principle of Least Surprise; old hands don't notice the delimiters and beginners don't know what to make of them. Have you looked at Rubular? It uses a second text field for the flags. I find that much less surprising. – Alan Moore May 20 '15 at 09:41

1 Answers1

6

There is an awesome site http://regex101.com for these needs! It describes regulars and allows you to test and debug them.

Your ones does match things like 4 (5 for the second one) values separated by commas and returns the last one as a signle matching group:

  • (?>...) are atomic groups. After they have matched once they won't leave it forever.
    • [^\,] matches any character except comma
    • [^\,]*\, means any number (even zero) of non-comma charaters, and then a sigle comma
  • (?>[^\,]*\,){3} means do that happend above 3 times
  • ([^\,]*)[\']? means one more word without commas as a group and possibly one more comma.

For example, in 1,,333,4,5 the first one will match 1,,333,4, and return 4 as matched group. The second one will find 1,,333,4,5 and 5 as group.

Edit: Even more description.

Regular expression have groups. These are parts or regular expressions that can have number quantifiers -- how many times to repeat them ({3}) and some options. Also, after regular has matched, we can find out what every group has matched.

Atomic ones, less talk, take as much forward as they can and never go back. Also, they can't be watched as described before. They are used here only due to perfomance reasons.

So, we need to take as a group the 4th word from comma-separated values. We will do it like this:

  • We will take 3 times ({3}) an atomic group ((?>...)):
    • Which takes a word -- any number of characters (*) of any non-comma character ([^\n])
      • [^...] means any symbol except described ones.
    • And a comma (\,) that separates that word from the next one
  • Now, our wanted word starts. We open a group ((...))
    • That will take a word as described above: [^\,]*
  • The is possibly one more comma, take it if there is one (\,? or [\,]?)
    • ? means 0 or 1 group before, here it's single comma.

So, it starts on first word in first atomic group, takes it all, then takes a comma. After that, it is repeated 2 times more. That takes 3 first words with their commas.

After that, one non-atomic group takes the 4th word.

Lapshin Dmitry
  • 1,084
  • 9
  • 28
  • I can offer even more description, if you really need. – Lapshin Dmitry May 19 '15 at 12:57
  • 1
    since you answered (very well) i suggest you upvote OP's question as it has many negative votes without much of a reason , +1 – Nikos M. May 19 '15 at 13:01
  • @LapshinDmitry This helps a lot but It would be good if u explain me more – Mounarajan May 19 '15 at 13:12
  • @Mounarajan, you will have to look into some tutorials and/or the links provided for further study plus experiment with the online tool, you will have to try a bit except if you have a specific question, this answer is very good (imo) – Nikos M. May 19 '15 at 13:24
  • @Mounarajan I editied the answer. That's pretty descriptive. If you need more support, you can try typing parts of regexp on [regex101](http://regex101.com). On the right, there will be a huge description of what's going on. – Lapshin Dmitry May 19 '15 at 13:29