Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Perl | Quantifiers in Regular Expression
Next article icon

Perl | pos() function in Regular Expression

Last Updated : 16 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

pos() function in Perl is used to return the position of the last match using the 'm' modifier in Regex. 
pos function can be used with the character classes in Regex to return a list of all the required substring positions in the given string. Global operator 'g' can also be used along with the 'm' modifier to search for the substring within the whole text.
 

Syntax: pos(String) 
Parameter: String after applying Regular Expression 
Returns: the position of the matched substring 
 


Example 1: Using a substring character 
 

Perl
#!/usr/bin/perl  # Program to print position of a substring $String = "Geeks For Geeks";  print" Position of 'G' in string:\n";  # Regex to search for substring # using m modifier while($String =~ m/G/g) {          # Finding the position of substring     # using pos() function     $position = pos($String);     print "$position\n"; } 

Output: 
Position of 'G' in string: 1 11

 

 
Example 2: Using a character class 
 

Perl
#!/usr/bin/perl  # Program to print position of a substring $String = "Geeks For Geeks";  print "Position of all Uppercase characters:\n";  # Regex to search for  # all the upper case characters  # using character class while($String =~ m/[A-Z]/g) {          # Finding the position of substring     # using pos() function     $position = pos($String);     print "$position, "; }  print "\nPosition of all Lowercase characters:\n";  # Regex to search for  # all the lower case characters  # using character class while($String =~ m/[a-z]/g) {          # Finding the position of substring     # using pos() function     $position = pos($String);     print "$position, "; } 

Output: 
Position of all Uppercase characters: 1, 7, 11,  Position of all Lowercase characters: 2, 3, 4, 5, 8, 9, 12, 13, 14, 15,

 

 
Example 3: Position of spaces 
 

Perl
#!/usr/bin/perl  # Program to print position of a substring $String = "Geeks For Geeks";  # Regex to search for  # all the spaces while($String =~ m/\s/g) {          # Finding the position of substring     # using pos() function     $position = pos($String);     print "$position\n"; } 

Output: 
6 10

 

  
Use of \G Assertion to match from specified position:
\G Assertion in Perl Regex is used to match the substring starting from a position specified by pos() function till the matching character specified in the regex. This will return the position of the first occurrence of the character specified by the 'm' modifier. 
Example: 
 

Perl
#!/usr/bin/perl  # Defining the default string $_ = "Geeks World is the best";  # Terminating character # using m modifier m/o/g;  # Specifying the starting position $position = pos();  # Using \G Assertion m/\G(.*)/g;  # Printing the position  # and the remaining string print "$position $1"; 

Output: 
8 rld is the best

 

In the above example, the position of the first occurrence of the matching substring is printed along with the remaining string. If there is a need to restart counting position for the next occurrence of the matching character, just store the remaining string that is in $1, into the default string.
Example: 
 

Perl
#!/usr/bin/perl  # Defining the default string $_ = "Geeks World is the best among all";  # Terminating character # using m modifier m/o/g;  # Specifying the starting position $position = pos();  # Using \G Assertion m/\G(.*)/g;  # Printing the position  # and the remaining string print "$position $1\n";  # To start counting from the matched character  # until the next possible match $_ = $1; m/o/g;  $position = pos();  # Using \G Assertion m/\G(.*)/g;  # Printing the position  # and the remaining string print "$position $1\n"; 

Output: 
8 rld is the best among all 19 ng all

 

Next Article
Perl | Quantifiers in Regular Expression

A

Abhinav96
Improve
Article Tags :
  • Perl
  • Perl-regex

Similar Reads

  • Perl | 'e' modifier in Regular Expression
    In Perl, the regular expression allows performing various operations on a given string with the use of suitable operators. These operators can perform operations like modification of string, substitution of other substrings, etc. Substitution of a substring in the given string is done with the use o
    3 min read
  • Perl | Regular Expressions
    Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text. Regex in Perl is linked to the host language and is not the same as in PHP, Python, etc. Sometimes it is termed as “Perl 5 Compatible Regular Expressions“. To use the Rege
    2 min read
  • Perl | Quantifiers in Regular Expression
    Perl provides several numbers of regular expression quantifiers which are used to specify how many times a given character can be repeated before matching is done. This is mainly used when the number of characters going to be matched is unknown. There are six types of Perl quantifiers which are give
    4 min read
  • Perl | Operators in Regular Expression
    Prerequisite: Perl | Regular Expressions The Regular Expression is a string which is the combination of different characters that provides matching of the text strings. A regular expression can also be referred to as regex or regexp. The basic method for applying a regular expression is to use of bi
    4 min read
  • Perl | Backtracking in Regular Expression
    In Perl, a Regular expression(a.k.a regexes or regexps or REs) is a way of describing a set of strings without having to list all strings in your program or simply we can say that it is a sequence of characters that are used for pattern matching. In Perl, regular expressions have different uses: Fir
    3 min read
  • Perl - Use of Capturing in Regular Expressions
    A regular expression or a regex is a string of characters that define the pattern that we are viewing. It is a special string describing a search pattern present inside a given text. Perl allows us to group portions of these patterns together into a subpattern and also remembers the string matched b
    3 min read
  • Perl | substr() function
    substr() in Perl returns a substring out of the string passed to the function starting from a given index up to the length specified. This function by default returns the remaining part of the string starting from the given index if the length is not specified. A replacement string can also be passe
    2 min read
  • Perl | Special Character Classes in Regular Expressions
    There are many different character classes implemented in Perl and some of them used so frequently that a special sequence is created for them. The aim of creating a special sequence is to make the code more readable and shorter. The Special Character Classes in Perl are as follows: Digit \d[0-9]: T
    4 min read
  • Perl | Useful String functions
    A string in Perl is a scalar variable and start with a ($) sign and it can contain alphabets, numbers, special characters. The string can consist of a single word, a group of words or a multi-line paragraph. The String is defined by the user within a single quote (‘) or double quote (“). Perl provid
    3 min read
  • Perl | Grouping and Alternation in Regex
    Regex or Regular Expressions are an important part of Perl Programming. It is used for searching the specified text pattern. In this, set of characters together form the search pattern. It is also known as regexp. Working with regex might become complex with more and more addition in functionalities
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences