Skip to content

Instantly share code, notes, and snippets.

@cdracars
Created November 9, 2021 22:30
Show Gist options
  • Save cdracars/33399d76996317af6a6da4d00bdb7492 to your computer and use it in GitHub Desktop.
Save cdracars/33399d76996317af6a6da4d00bdb7492 to your computer and use it in GitHub Desktop.
Very basic python script to update form_state from Drupal 7 to Drupal 9.
#!/usr/bin/env python3
import re
filename = 'fsp_competitive_cross_reference.cut_n_paste.inc'
# Read in the file
with open(filename, 'r') as file:
filedata = file.read()
# print(filedata)
values = re.findall("\$form_state\['values'\].*\]", filedata)
temp = re.sub(r"(\$form_state\['values'\]\[)('.*')(\])", r'$form_state->getValue(\2)', filedata)
print(temp)
@worxco
Copy link

worxco commented Dec 2, 2021

regex is a bit greedy - so on line 12 your closing .] would consume all characters until the LAST ] and not the first ] (if there was more than one....)
that is why I used, (in the vim description) '[^']
'
This said find a single quote, then all following characters that are NOT a single quote, then a closing single quote....
This is NOT greedy and stops at the first closing quote.

Of course, if you had perfect code and only one instance of things per line.... no problem. but coders take shortcuts sometimes and you cannot be assured that is always going to be true.

@worxco
Copy link

worxco commented Dec 2, 2021

damn thing pulled my astericks.... I should have probably used somthing that indicated this was code....
regex is a bit greedy - so on line 12 your closing .*\] would consume all characters until the LAST ] and not the first ] (if there was more than one....) that is why I used, (in the vim description) '[^']*'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment