How to write a regular expression: find all the words in which the letter "a" stands on the 5th place and return the letter that stands on the 6th?

Place is the ordinal number of a letter in a word.

    2 answers 2

    why is there a regular season? Loop through

    if(string.length>5&&string[4]=='a')console.log(string[5])); 
    • Thank you, hedgehog !! - user208916
    • corrected indices in substr - rjhdby
    • one
      string[4] can be easier - ReinRaus

    If by "words" we mean lines in an array, then here:

     ^.{4}a(.) 

    Matchet four any characters from the beginning of the line ( ^ ), then the letter a , and then any character, and captures it in the first group.

    If words mean exactly the words in the line where the word is a sequence of non-space characters, then a little more complicated:

     (?:^|\s)[^\s]{4}a([^\s]) 

    Matchet is either the beginning of a line or a space character in order to catch the beginning of a word. In order to realize this choice, a non-addictive group is used, since we are not interested in this symbol. Then everything is almost the same as in the past regexpe: four non-space characters are matched, then the letter a , and then any non-space character that is captured in the first group. Instead of any ( . ) Matched non-whitespace characters ( [^\s] , or you can instead use \S if the language supports) in order to avoid jumping to the next word during the matching.

    The way of using the second regular schedule depends on the language. For example, in JavaScript it will be somewhere like this:

     function test(words) { var re = /(?:^|\s)[^\s]{4}a([^\s])/g var match = re.exec(words) while (match) { console.log('Word: "' + match[0] + '", letter: "' + match[1] + '"') match = re.exec(words) } } test('1234a5 000 ____aZ smth') // Prints: // Word: "1234a5", letter: "5" // Word: " ____aZ", letter: "Z"