Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parse string with regex

Status
Not open for further replies.

clavrg

Programmer
Aug 16, 2005
3
US
--- 1 ---
I need to execute a regex against a string, and retrieve values of the groups.
E.g.:
regex: ^(?[0-9]+)Z(?[A-Z\.])$
string: 003ZAS.S
expected result:
group 1: 003
group 2: AS.S

match function will do that, but it requires extensions, is there a way to do it without using extensions?


--- 2 ---
Specifically, I need to parse version in format:
<major>.<minor>.<build>.<revision>

Split works just fine, but I would like to do it with regex instead.


--- 3 ---
What is the "nice" way of parsting this string?

Regards,
Roman V. Gavrilov
 
Standard Awk does not "capture" the values of groups in regular expressions. However, Gawk has gensub, which will let you do what you want (I think; haven't used it myself).

If you can't use Gawk:
Code:
BEGIN {
  string = "003ZAS.S"
  group1 = string; sub(/[^0-9]+/, "", group1)
  group2 = string; sub(/^.*[0-9]Z/, "", group2)
  print group1, group2
}
Specifically, I need to parse version in format:
<major>.<minor>.<build>.<revision>

Split works just fine, but I would like to do it with regex instead.
What's wrong with using split? It's probably faster than capturing groups in a regular expression.
 
Thank you, futurlet,

Will check sub once have a chance.

Regarding using split - it will work for this particular case, though it is not as general as regex. Consider situation if I will decide to change format to something more exotic, e.g. 13.30-3.b#some comments with regex fixed quickly it will be.

Regards,
Roman V. Gavrilov
 
Consider situation if I will decide to change format to something more exotic, e.g. 13.30-3.b#some comments
Code:
split("13.30-3.b#some comments",a,"[-.#]")
 
<comment>#<major>.<minor>-<build>.<revision_letter>
something-with-dashes.and.dots#13.30-3.b
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top